This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userDice.cpp
153 lines (121 loc) · 3.35 KB
/
userDice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "userDice.hpp"
#include <iostream>
// UserDice implementation
UserDice::UserDice() : m_btn_add("Add")
{
m_head = new Head;
//m_head->next = NULL;
m_head->m_next = new UserDie("Woot", 10, m_head);
m_btn_add.signal_clicked().connect(sigc::mem_fun(*this,
&UserDice::add_new_noname));
m_btn_add.set_size_request(10, 10);
pack_start(m_scrolledWindow, Gtk::PACK_EXPAND_WIDGET, 10);
m_scrolledWindow.add(m_vbox);
m_vbox.pack_start(*m_head->m_next, Gtk::PACK_SHRINK );
pack_start(m_btn_add, Gtk::PACK_SHRINK, 5);
m_head->m_next->show();
m_btn_add.show();
m_scrolledWindow.show();
m_vbox.show();
}
UserDice::~UserDice()
{
do
{
if (m_head->m_next == NULL) // If there are no nodes to delete
break; // Don't do any deleting.
// Look at the first node.
UserDie *iter = m_head->m_next;
// m_head->m_next looks at the next one.
m_head->m_next = iter->m_next;
// Delete the current one
delete iter;
}
while (m_head->m_next); // While there are still nodes
// Deallocate m_head
delete m_head;
}
void UserDice::add_new_noname()
{
add_new("Foo", 20);
}
// Append a new userDie to the list.
void UserDice::add_new(const char *name, int sides)
{
UserDie *iter = m_head->m_next;
if (!iter)
{
// This is the first one
iter = new UserDie(name, sides, m_head);
}
else
{
// Find the end.
for (iter = m_head->m_next; iter->m_next != NULL; iter = iter->m_next)
; // Nothing
// Point end to new node
iter->m_next = new UserDie(name, sides, m_head);
iter->m_next->m_prev = iter; // Point to the previous node
}
// Pack new node.
m_vbox.pack_start(*iter, Gtk::PACK_SHRINK , 2);
iter->show();
}
////////////////////////////////////////////////////////////
// UserDie implementation
////////////////////////////////////////////////////////////
// Constructor
UserDice::UserDie::UserDie(const char *name, int sides, Head *parent)
: m_button(name), m_btn_delete("X"), m_die(sides)
{
m_parent_head = parent; // Point to head.
m_hbox.set_size_request(200, 10);
m_button.set_size_request(200, 10);
// Connect the delete button to the remove() function.
m_btn_delete.signal_clicked().connect(sigc::mem_fun(*this,
&UserDice::UserDie::remove));
// test roll stuffs
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&UserDice::UserDie::rollDie));
// test function for the dialog
m_btn_dialog.signal_clicked().connect(sigc::mem_fun(*this,
&UserDice::UserDie::addDialog));
m_next = NULL;
m_prev = NULL;
pack_start(m_hbox, Gtk::PACK_SHRINK );
m_hbox.pack_start(m_button, Gtk::PACK_EXPAND_PADDING);
m_hbox.pack_start(m_btn_dialog, Gtk::PACK_EXPAND_PADDING);
m_hbox.pack_start(m_btn_delete, Gtk::PACK_EXPAND_PADDING);
m_btn_dialog.show();
m_button.show();
m_btn_delete.show();
m_hbox.show();
}
// Deletes itself
void UserDice::UserDie::remove()
{
// If this is the first node
if (!m_prev)
m_parent_head->m_next = NULL;
// If there isn't a next node, make previous node point to nowhere.
if (!m_next)
m_prev = NULL;
else // There is a next node
{
// Make the previous node point to the next node.
m_prev->m_next = m_next;
// Make the next node point to the previous node.
m_next->m_prev = m_prev;
}
// Delete this
delete this;
}
void UserDice::UserDie::addDialog()
{
// stuff to open a dialog window here
m_userDieSetting.run();
}
void UserDice::UserDie::rollDie()
{
std::cout << "pressed";
}