Text - Elements - Hag GUI for Cobra
Text elements are individually styled blocks of text, supporting word-wrapping, \n line breaks, tooltips and hyperlinks.
Text elements expose the following read-only variables:
hText.x: Integer - Text x position
hText.y: Integer - Text y position
hText.w: Integer - Text width
hText.h: Integer - Text height
hText.enabled: Boolean - If text is enabled
hText.visible: Boolean - If text is visible
hText.href: String - Text hyperlink (default is an empty string)
For a ready-to-run example, see Example_Text or Example2D in the Examples download.
Example:
Uses
hagC2D,
cobra2D,
keyset
Var
y: Integer
frmMain: ^forms
txtMain: ^hTexts
txtLink1: ^hTexts
txtLink2: ^hTexts
txtLink3: ^hTexts
btnClose: ^buttons
background: Element
cursor_hand : Element
cursor_text : Element
tooltip : Element
Procedure UpdateMyCursor()
Var
lastMouse : Static Integer
lastTip : Static String
Begin
Case hag_mouse of
(HAG_MOUSE_NORMAL):
If lastMouse <> HAG_MOUSE_NORMAL then
ShowMouse()
SpriteVisible(cursor_hand,FALSE)
SpriteVisible(cursor_text,FALSE)
lastMouse = HAG_MOUSE_NORMAL
Endif
(HAG_MOUSE_HAND):
If lastMouse <> HAG_MOUSE_HAND then
HideMouse()
SpriteVisible(cursor_hand,TRUE)
SpriteVisible(cursor_text,FALSE)
lastMouse = HAG_MOUSE_HAND
Endif
PositionSprite(cursor_hand,MouseX,MouseY)
(HAG_MOUSE_TEXT):
If lastMouse <> HAG_MOUSE_TEXT then
HideMouse()
SpriteVisible(cursor_hand,FALSE)
SpriteVisible(cursor_text,TRUE)
lastMouse = HAG_MOUSE_TEXT
Endif
PositionSprite(cursor_text,MouseX,MouseY)
EndCase
{ Tooltip }
If hag_tooltip <> lastTip then
If hag_tooltip <> "" then
CLS(ToRGBA(0,0,0,0),tooltip)
PenColor(ToRGBA(0,0,0),tooltip)
SetFont("arial",8,0,tooltip)
Rect(0,0,TextWidth(hag_tooltip,tooltip)+10,TextHeight(hag_tooltip,tooltip)+4,ToRGBA(255,255,225),TRUE,tooltip)
Rect(0,0,TextWidth(hag_tooltip,tooltip)+10,TextHeight(hag_tooltip,tooltip)+4,ToRGBA(0,0,0),FALSE,tooltip)
SpriteVisible(tooltip,TRUE)
Text(5,2,hag_tooltip,tooltip)
Else
SpriteVisible(tooltip,FALSE)
Endif
lastTip = hag_tooltip
Endif
PositionSprite(tooltip,MouseX+15,MouseY+10)
End
Begin
SetAppName("Text example")
OpenScreen(320, 240, 32, FALSE, COB_SHOWBORDER+COB_SHOWCLOSE)
Randomize(Millisecs)
background = CreateSprite(320,240)
Cls(ToRGBA(235,233,237),background) ; Flip
// Init GUI
HagBaseDir("hag\")
HagInit(320, 240, 1) // Start at sprite index 1 because of the background sprite
HagLoadGuiTheme("xp")
// Set up GUI Elements
frmMain = CreateForm()
txtMain = CreateText(frmMain, 20, 20, "This is an example of the Hag GUI using text elements.\n\nText elements automatically use text-wrapping and support /n linebreaks.\n\nAlso, hyperlinks!\n\n", 280)
y = txtMain.y + txtMain.h // Get end of the Text block
txtLink1 = CreateHyperlink(frmMain, 20, y, "Visit Google", "http://www.google.co.uk/")
txtLink2 = CreateHyperlink(frmMain, 20, y+20, "Visit TopHatStuff", "http://www.tophatstuff.co.uk/")
txtLink3 = CreateHyperlink(frmMain, 20, y+40, "Hag GUI Documentation", "http://www.tophatstuff.co.uk/?p=42")
btnClose = CreateButton(frmMain, 250, 200, 50, "Close")
FocusButton(btnClose)
HagUpdateOnce()
// Mouse
tooltip = CreateSprite(320,20)
SpriteVisible(tooltip,FALSE)
cursor_hand = LoadSprite("img\cursor_hand.png")
SpriteVisible(cursor_hand,FALSE)
cursor_text = LoadSprite("img\cursor_text.png")
SpriteVisible(cursor_text,FALSE)
While ExitRequested = FALSE
HagUpdateAll_AutoKeys()
UpdateMyCursor()
If ButtonClicked(btnClose) then RequestExit
If KeyHits(VK_ESCAPE) > 0 then RequestExit
Flip
Pause(1)
Wend
HagFreeAll()
CloseScreen()
End
Stay Subscribed
RSS feed or get updates by e-mail.
, subscribe to theYou can also contact me directly - I make an effort to reply to every e-mail.