summaryrefslogtreecommitdiff
path: root/simplegui.mod/simplegui.bmx
blob: deb6dfcafa42b0460553a45822681c4c83ec4467 (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
Module noddybox.simplegui

ModuleInfo "Framework: (Very) Simple GUI"
ModuleInfo "Copyright: Public Domain"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"

' $Id$

Strict
Import brl.Max2D
Import brl.Basic
Import noddybox.bitmapfont

Type TWidget Abstract

	Field	text:String
	Field	x:Int
	Field	y:Int
	Field	w:Int
	Field	h:Int
	Field	font:TBitmapFont
	Field	owner:TGUIHandler
	Field	callback()
	
	Method HandleKey(key:String)
	End Method
	
	Method MouseEnter()
	End Method

	Method MouseLeave()
	End Method

	Method HandleClick()
	End Method
	
	Method Draw() Abstract
End Type

Type TLabel Extends TWidget
	Function Create:TLabel(font:TBitmapFont,x:Int, y:Int, text:String)
		Local o:TLabel=New TLabel
		o.font=font
		o.x=x
		o.y=y
		o.w=font.TextWidth(text)+2
		o.h=font.MaxHeight()+1
		o.text=text
		Return o
	End Function
	
	Method Draw()
		font.Draw(text,x+1,y+1)
	End Method
End Type

Type TText Extends TWidget
	Field	maxlen:Int
	
	Function Create:TText(font:TBitmapFont,x:Int, y:Int, text:String, maxlen:Int)
		Local o:TText=New TText
		o.font=font
		o.x=x
		o.y=y
		o.maxlen=maxlen
		o.w=font.MaxWidth()*(maxlen+1)+2
		o.h=font.MaxHeight()+2
		o.text=text
		Return o
	End Function
	
	Method Draw()
		Local s:String=text
		
		If owner.GetFocus()=Self
			SetColor(128,128,128)
			s:+"_"
		Else
			SetColor(64,64,64)
		EndIf
		
		DrawRect(x,y,w,h)
		
		font.Draw(s,x+1,y+1)
	End Method
End Type

Type TGUIHandler

	' These are private
	'
	Field 	m_widgets:TList
	Field	m_focus:TWidget

	' Creates a new GUI handler
	'	
	Function Create:TGUIHandler()
		Local o:TGUIHandler
		
		o=New TGUIHandler
		
		o.m_widgets=CreateList()
		o.m_focus=Null

		Return o
	End Function
	
	' Register a widget
	'
	Method Register(w:TWidget)
		m_widgets.AddLast(w)
		w.owner=Self
	End Method
	
	' Set the keyboard focus (null for none)
	'
	Method SetFocus(w:TWidget)
		m_focus=w
	End Method
	
	' Get the keyboard focus (null for none)
	'
	Method GetFocus:TWidget()
		Return m_focus
	End Method
	
	' Perform a loop and any necessary events
	'
	Method EventLoop()
		For Local w:TWidget=EachIn m_widgets
			w.Draw()
		Next
	End Method
End Type