summaryrefslogtreecommitdiff
path: root/button.go
blob: e7ed2777a85752ca669fbb95bb92cf73c7ffe2dd (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gui

import "log"
// import "reflect"
import "github.com/andlabs/ui"
import _ "github.com/andlabs/ui/winmanifest"
// import "github.com/davecgh/go-spew/spew"


// This is the default mouse click handler
// Every mouse click that hasn't been assigned to
// something specific will fall into this routine
// By default, all it runs is the call back to
// the main program that is using this library
//
// This routine MUST be here as this is how the andlabs/ui works
// This is the raw routine passed to every button in andlabs libui / ui
//
// There is a []GuiButton which has all the buttons. We search
// for the button and then call the function below
//
func defaultButtonClick(button *ui.Button) {
	log.Println("gui.defaultButtonClick() LOOK FOR BUTTON button =", button)
	for key, foo := range Data.AllButtons {
		if (Config.Debug) {
			log.Println("gui.defaultButtonClick() Data.AllButtons =", key, foo)
			// spew.Dump(foo)
		}
		if Data.AllButtons[key].B == button {
			log.Println("\tgui.defaultButtonClick() BUTTON MATCHED")
			guiButtonClick(Data.AllButtons[key])
			return
		}
	}
	log.Println("\tgui.defaultButtonClick() ERROR: BUTTON NOT FOUND")
	if (Config.Debug) {
		panic("gui.defaultButtonClick() ERROR: UNMAPPED ui.Button")
	}
}

func guiButtonClick(button *GuiButton) {
	log.Println("\tgui.guiButtonClick() button.Name =", button.Name)
	if button.Custom != nil {
		log.Println("\tgui.guiButtonClick() DOING CUSTOM FUNCTION")
		button.Custom(button)
		return
	}
	if (Data.MouseClick != nil) {
		Data.MouseClick(button)
	} else {
		log.Println("\tgui.guiButtonClick() IGNORING BUTTON. MouseClick() is nil")
	}
}

func (n *Node) AddButton(name string, custom func(*Node)) *Node {
	if (n.Toolkit == nil) {
		log.Println("gui.Node.AppendButton() filed node.Toolkit == nil")
		panic("gui.Node.AppendButton() filed node.Toolkit == nil")
		return n
	}
	/*
	button := ui.NewButton(name)
	log.Println("reflect.TypeOF(uiBox) =", reflect.TypeOf(n.uiBox))
	log.Println("reflect.TypeOF(uiButton) =", reflect.TypeOf(button))
	n.uiBox.Append(button, false)
	n.uiButton = button
	*/

	newNode := n.New(name)
	newNode.Toolkit = n.Toolkit.NewButton(name)
	newNode.Toolkit.Custom = func() {
		log.Println("gui.AppendButton() Button Clicked. Running custom()")
		custom(newNode)
	}
	newNode.custom = custom

	/*
	button.OnClicked(func(*ui.Button) {
		log.Println("gui.AppendButton() Button Clicked. Running custom()")
		custom(newNode)
	})
	*/
	// panic("AppendButton")
	// time.Sleep(3 * time.Second)
	return newNode
}

func (n *Node) CreateButton(custom func(*GuiButton), name string, values interface {}) *Node {
	newNode := n.AddBox(Xaxis, "test CreateButton")
	box := newNode.FindBox()
	if (box == nil) {
		panic("node.CreateButton().FindBox() == nil")
	}
	newUiB := ui.NewButton(name)
	newUiB.OnClicked(defaultButtonClick)

	var newB *GuiButton
	newB		= new(GuiButton)
	newB.B		= newUiB
	if (box.UiBox == nil) {
		log.Println("CreateButton() box.Window == nil")
		// ErrorWindow(box.Window, "Login Failed", msg) // can't even do this
		panic("maybe print an error and return nil? or make a fake button?")
	} else {
		// uibox := box.UiBox
		// uibox.Append(newUiB, true)
	}
	newB.Box	= box
	newB.Custom	= custom
	newB.Values	= values

	Data.AllButtons	= append(Data.AllButtons, newB)

	box.Append(newB.B, false)
	return newNode
}

func (n *Node) NewButton(box *GuiBox, custom func(*GuiButton), name string, values interface {}) *GuiButton {
	return CreateButton(box, custom, name, values)
}

func CreateButton(box *GuiBox, custom func(*GuiButton), name string, values interface {}) *GuiButton {
	newUiB := ui.NewButton(name)
	newUiB.OnClicked(defaultButtonClick)

	var newB *GuiButton
	newB		= new(GuiButton)
	newB.B		= newUiB
	if (box.Window == nil) {
		log.Println("CreateButton() box.Window == nil")
		// ErrorWindow(box.Window, "Login Failed", msg) // can't even do this
		panic("maybe print an error and return nil? or make a fake button?")
	}
	newB.Box	= box
	newB.Custom	= custom
	newB.Values	= values

	Data.AllButtons	= append(Data.AllButtons, newB)

	box.Append(newB.B, false)
	return newB
}

func CreateFontButton(box *GuiBox, action string) *GuiButton {
        // create a 'fake' button entry for the mouse clicks
	var newGB	GuiButton
	newGB.Name	= "FONT"
	newGB.FB	= ui.NewFontButton()
	newGB.Box	= box
	Data.AllButtons	= append(Data.AllButtons, &newGB)

	newGB.FB.OnChanged(func (*ui.FontButton) {
		log.Println("FontButton.OnChanged() START mouseClick(&newBM)", newGB)
		if (Data.MouseClick != nil) {
			Data.MouseClick(&newGB)
		}
	})
	return &newGB
}

func CreateColorButton(box *GuiBox, custom func(*GuiButton), name string, values interface {}) *GuiButton {
        // create a 'fake' button entry for the mouse clicks
	var newCB	GuiButton
	newCB.Name	= name
	newCB.CB	= ui.NewColorButton()
	newCB.Box	= box
	newCB.Custom	= custom
	newCB.Values	= values

	Data.AllButtons	= append(Data.AllButtons, &newCB)

	newCB.CB.OnChanged(func (*ui.ColorButton) {
		log.Println("ColorButton.OnChanged() START Color Button Click")
		r, g, b, a := newCB.CB.Color()
		log.Println("ColorButton.OnChanged() Color() =", r, g, b, a)
		if (newCB.Custom != nil) {
			newCB.Custom(&newCB)
		} else if (Data.MouseClick != nil) {
			Data.MouseClick(&newCB)
		}
	})
	box.Append(newCB.CB, false)
	return &newCB
}