[B+] ScrollBox-Funktion

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

KnorxThieus

Betreff: [B+] ScrollBox-Funktion

BeitragMo, Mai 20, 2013 12:33
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo,


ich habe mal eine Funktion für Sliders gebaut, damit man die nicht immer von hand abfragen muss. Hoffe, sowas gab es noch nicht.
BlitzBasic: [AUSKLAPPEN]
SeedRnd MilliSecs()

;ScrollBox by KnorxThieus
;°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
;Functions:
;CreateScrollBox(x, y, visible_width, visible_height, total_width, total_height, group[, style])
; creates a scrollbox
; PARAMETERS:
; x, y, visible_width, visible_height --> coordinates
; total_width, total_height --> total size
; group --> parent
; style = 1 --> border
; RETURN:
; ScrollBox' panel, which is parent for its gadgets
;UpdateScrollBox(box)
; actualizes visible field of box with values of sliders, actualizes sliders' postitions for mousewheel
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; RETURN:
; /
;SetScrollBoxWheel(box[, direction][, value])
; sets whats happens if mouse is wheeled
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; direction:
; +1 --> horizontal scrolling activated
; +2 --> vertical scrolling activated
; value --> value pro 1 wheel
; RETURN:
; /
;GetScrollBoxWheel(box)
; return set scrollbox wheel
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; RETURN:
; if value for vertical wheel exists, that value.
; else, if value for horizontal wheel exists, that horizontal value.
;SetScrollBoxRange(box, total_width, total_height, [visible_width, visible_height])
; set visible and total size of scrollbox
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; total_width, total_height --> total size
; visible_width, visible_height --> coordinates
; RETURN:
; /
;GetScrollBoxRange(box, coordinate)
; returns total size of scrollbox
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; coordinate = 0 --> selects direction width for return
; coordinate = 1 --> selects direction height for return
; RETURN:
; size of selected direction
;ScrollBoxGadget(box)
; returns visible gadget of scrollbox
; gadgets should be created into scrollbox BOX; gadget actions like hiding the scrollbox should be employed on scrollbox GADGET which's returned here
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; RETURN:
; explaned on the top
;SetScrollBoxScrolled(box, x, y[, style])
; scrolls the scrollbox on position x, y
; PARAMETERS:
; box --> gadget returned by CreateScrollBox
; x, y --> scroll coordinates
; style = 1 --> x and y are added to actual scroll coordinates.
; RETURN:
; /
;GetScrollBoxScrolled(box, coordinate = 0)
; returns scrolled position of scrollbox
; PARAMETERS:
; box --> I think now we all know what means the parameter box :-)
; coordinate = 0 --> selects x-axis for return
; coordinate = 1 --> selects y-axis for return
; RETURN:
; scroll value of selected axis
;###################################################################
;Don't forget writing type ScrollBox_data into your code!
;It's recommendable copying ScrollBox.decls to your userlibs!

Type ScrollBox_data
Field box, panel, ppanel
Field x, y, visible_width, visible_height, total_width, total_height, group, style
Field slider_h, slider_v
Field wheelx, wheely
End Type

Function CreateScrollBox(x, y, visible_width, visible_height, total_width, total_height, group, style = 0)
ScrollBox.ScrollBox_data = New ScrollBox_data
ScrollBox\x = x
ScrollBox\y = y
ScrollBox\visible_width = visible_width
ScrollBox\visible_height = visible_height
ScrollBox\total_width = total_width + 15
ScrollBox\total_height = total_height + 15
ScrollBox\group = group
ScrollBox\style = style
ScrollBox\box = CreatePanel(ScrollBox\x, ScrollBox\y, ScrollBox\visible_width, ScrollBox\visible_height, ScrollBox\group, ScrollBox\style) ;-15 removed
ScrollBox\ppanel = CreatePanel(0, 0, ClientWidth(ScrollBox\box) - 15, ClientHeight(ScrollBox\box) - 15, ScrollBox\box)
ScrollBox\panel = CreatePanel(0, 0, ScrollBox\total_width, ScrollBox\total_height, ScrollBox\ppanel)
ScrollBox\slider_h = CreateSlider(0, ClientHeight(ScrollBox\box) - 15, ClientWidth(ScrollBox\box) - 15, 15, ScrollBox\box, 1)
ScrollBox\slider_v = CreateSlider(ClientWidth(ScrollBox\box) - 15, 0, 15, ClientHeight(ScrollBox\box) - 15, ScrollBox\box, 2)
SetSliderRange ScrollBox\slider_h, ScrollBox\visible_width, ScrollBox\total_width
SetSliderRange ScrollBox\slider_v, ScrollBox\visible_height, ScrollBox\total_height
If ScrollBox\total_width <= ScrollBox\visible_width
HideGadget ScrollBox\slider_h
EndIf
If ScrollBox\total_height <= ScrollBox\visible_height
HideGadget ScrollBox\slider_v
EndIf
Return ScrollBox\panel
End Function

Function UpdateScrollBox(box)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If EventID() = $204
wx = EventData() * ScrollBox\wheelx
wy = EventData() * ScrollBox\wheely
EndIf
SetSliderValue ScrollBox\slider_v, SliderValue(ScrollBox\slider_v) - wy
SetSliderValue ScrollBox\slider_h, SliderValue(ScrollBox\slider_h) - wx
SetGadgetShape ScrollBox\panel, 0 - SliderValue(ScrollBox\slider_h), 0 - SliderValue(ScrollBox\slider_v), GadgetWidth(ScrollBox\panel), GadgetHeight(ScrollBox\panel)
End Function

Function SetScrollBoxWheel(box, direction = 0, value = 0)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If value = 0 Then value = ScrollBox\wheely
; Select direction
; Case 0
; ScrollBox\wheelx = 0
; ScrollBox\wheely = 0
; Case 1
; ScrollBox\wheelx = value
; ScrollBox\wheely = 0
; Case 2
; ScrollBox\wheelx = 0
; ScrollBox\wheely = value
; Case 3
; ScrollBox\wheelx = value
; ScrollBox\wheely = value
; End Select
ScrollBox\wheelx = GetBit(direction, 0) * value
ScrollBox\wheely = GetBit(direction, 1) * value
End Function

Function GetScrollBoxWheel(box)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If ScrollBox\wheely
Return ScrollBox\wheely
ElseIf ScrollBox\wheelx
Return ScrollBox\wheelx
EndIf
End Function

Function SetScrollBoxRange(box, total_width, total_height, visible_width = 0, visible_height = 0)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If visible_width <> 0 Then ScrollBox\visible_width = visible_width
If visible_height <> 0 Then ScrollBox\visible_height = visible_width
If group <> 0 Then ScrollBox\group = group
ScrollBox\total_width = total_width + 15
ScrollBox\total_height = total_height + 15
SetGadgetShape ScrollBox\box, ScrollBox\x, ScrollBox\y, ScrollBox\visible_width, ScrollBox\visible_height
SetGadgetShape ScrollBox\ppanel, 0, 0, ClientWidth(ScrollBox\box) - 15, ClientHeight(ScrollBox\box) - 15
SetGadgetShape ScrollBox\panel, 0, 0, ScrollBox\total_width, ScrollBox\total_height
SetSliderRange ScrollBox\slider_h, ClientWidth(ScrollBox\box), ScrollBox\total_width
SetSliderRange ScrollBox\slider_v, ClientHeight(ScrollBox\box), ScrollBox\total_height
If ScrollBox\total_width <= ScrollBox\visible_width
HideGadget ScrollBox\slider_h
Else
ShowGadget ScrollBox\slider_h
EndIf
If ScrollBox\total_height <= ScrollBox\visible_height
HideGadget ScrollBox\slider_v
Else
ShowGadget ScrollBox\slider_v
EndIf
End Function

Function GetScrollBoxRange(box, coordinate)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If coordinate = 0 Then Return ScrollBox\total_width
If coordinate = 1 Then Return ScrollBox\total_height
End Function

Function ScrollBoxGadget(box)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
Return ScrollBox\box
End Function

Function SetScrollBoxScrolled(box, x, y, style = 0)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If style
x = SliderValue(ScrollBox\slider_h) + x
y = SliderValue(ScrollBox\slider_v) + y
EndIf
SetSliderValue ScrollBox\slider_h, x
SetSliderValue ScrollBox\slider_v, y
SetGadgetShape ScrollBox\panel, 0 - SliderValue(ScrollBox\slider_h), 0 - SliderValue(ScrollBox\slider_v), GadgetWidth(ScrollBox\panel), GadgetHeight(ScrollBox\panel)
End Function

Function GetScrollBoxScrolled(box, coordinate = 0)
For ScrollBox.ScrollBox_data = Each ScrollBox_data
If ScrollBox\panel = box
ScrollBox = ScrollBox.ScrollBox_data
Exit
EndIf
Next
If coordinate = 0 Then Return SliderValue(ScrollBox\slider_v)
If coordinate = 1 Then Return SliderValue(ScrollBox\slider_h)
End Function







win = CreateWindow("ScrollBox", 200, 200, 400, 400, Desktop(), 13)

m1 = CreateMenu("Linke ScrollBox", 1, WindowMenu(win))
m2 = CreateMenu("Rechte ScrollBox", 1, WindowMenu(win))
m11 = CreateMenu("Scrollgeschwindigkeit abrufen", 11, m1)
m12 = CreateMenu("Scrollgeschwindigkeit ändern", 12, m1)
m13 = CreateMenu("Scrollen auf...", 13, m1)
CreateMenu("", 0, m1)
m14 = CreateMenu("ScrollBox verstecken", 14, m1)
m21 = CreateMenu("Scrollgeschwindigkeit abrufen", 21, m2)
m22 = CreateMenu("Scrollgeschwindigkeit ändern", 22, m2)
m23 = CreateMenu("Scrollen auf...", 23, m2)
CreateMenu("", 0, m2)
m24 = CreateMenu("ScrollBox verstecken", 24, m2)

UpdateWindowMenu win

Repeat
p$ = RequestFile$("Wähle ein Foto!", "jpg,bmp,png")
If p$ = ""
If Not Confirm("Das Bild ist kein Bild!", 1)
End
EndIf
EndIf
Until FileType(p$) = 1

sb1 = CreateScrollBox(5, 5, ClientWidth(win) / 2 - 10, ClientHeight(win) - 10, ImageWidth(LoadImage(p$)), ImageHeight(LoadImage(p$)), win, 1)

picture1 = CreatePanel(0, 0, ImageWidth(LoadImage(p$)), ImageHeight(LoadImage(p$)), sb1)
SetPanelImage picture1, p$

repl = CreateButton("Bild neu aussuchen", 5, 5, 100, 25, picture1)


sb2 = CreateScrollBox(ClientWidth(win) / 2, 5, ClientWidth(win) / 2 - 10, ClientHeight(win) - 10, 1060, 310, win)
Dim buttons(9, 9)

picture2 = CreatePanel(0, 0, 1060, 310, sb2)
For a = 0 To 9
For b = 0 To 9
buttons(a, b) = CreateButton(Chr$(Rand(0, 255)) + Chr$(Rand(0, 255)) + Chr$(Rand(0, 255)), a * 105 + 5, b * 30 + 5, 100, 25, picture2)
Next
Next



Repeat
Select WaitEvent(0)
Case $204
UpdateScrollBox sb1
UpdateScrollBox sb2
Case $401
Select EventSource()
Case enterb
d = Proceed("Welche Richtung?" + Chr$(10) + "Ja = Beide, Vertikal = Nein, Horizontal = Abbrechen") + 2
SetScrollBoxWheel(edited, d, Int(TextFieldText$(enterf)))
edited = 0
FreeGadget subwin
EnableGadget win
ActivateGadget win
Case enterb0
SetScrollBoxScrolled(edited, Int(TextFieldText$(enterf1)), Int(TextFieldText$(enterf2)))
edited = 0
FreeGadget subwin
EnableGadget win
ActivateGadget win
Case repl
Repeat
p$ = RequestFile$("Wähle ein Foto!", "jpg,bmp,png")
If p$ = ""
If Not Confirm("Das Bild ist kein Bild!", 1)
End
EndIf
EndIf
Until FileType(p$) = 1
SetScrollBoxRange(sb1, ImageWidth(LoadImage(p$)), ImageHeight(LoadImage(p$)))
SetGadgetShape picture1, 0, 0, ImageWidth(LoadImage(p$)), ImageHeight(LoadImage(p$))
SetPanelImage picture1, p$
SetGadgetShape repl, 5, 5, 100, 25
Default
For a = 0 To 9
For b = 0 To 9
If buttons(a, b) = EventSource()
Notify GadgetText$(buttons(a, b)) + " sagt:" + Chr$(10) + "Autsch! Das tut weh!"
EndIf
Next
Next
End Select
UpdateScrollBox sb1
UpdateScrollBox sb2
Case $803
End
Case $1001
Select EventData()
Case 11, 21
If Left$(EventData(), 1) = 1 Then edited = sb1 Else edited = sb2
Notify GetScrollBoxWheel(edited)
edited = 0
Case 12, 22
If Left$(EventData(), 1) = 1 Then edited = sb1 Else edited = sb2
DisableGadget win
subwin = CreateWindow("Scrollgeschwindigkeit von ScrollBox " + Left$(EventData(), 1) + " ändern", GadgetX(win) + 13, GadgetY(win) + 36, 106, 54, win, 1)
enterf = CreateTextField(0, 0, 75, 25, subwin)
SetGadgetText enterf, GetScrollBoxWheel(edited)
enterb = CreateButton("OK", 75, 0, 25, 25, subwin, 4)
ActivateGadget enterf
Case 13, 23
If Left$(EventData(), 1) = 1 Then edited = sb1 Else edited = sb2
DisableGadget win
subwin = CreateWindow("ScrollBox " + Left$(EventData(), 1) + " scrollen auf...", GadgetX(win) + 13, GadgetY(win) + 36, 106, 54, win, 1)
enterf1 = CreateTextField(0, 0, 75 / 2, 25, subwin)
enterf2 = CreateTextField(75 / 2, 0, 75 / 2, 25, subwin)
SetGadgetText enterf1, GetScrollBoxScrolled(edited, 1)
SetGadgetText enterf2, GetScrollBoxScrolled(edited, 0)
enterb0 = CreateButton("OK", 75, 0, 25, 25, subwin, 4)
ActivateGadget enterf1
Case 14
If Not MenuChecked(m14)
CheckMenu m14
HideGadget ScrollBoxGadget(sb1)
Else
UncheckMenu m14
ShowGadget ScrollBoxGadget(sb1)
EndIf
UpdateWindowMenu win
Case 24
If Not MenuChecked(m24)
CheckMenu m24
HideGadget ScrollBoxGadget(sb2)
Else
UncheckMenu m24
ShowGadget ScrollBoxGadget(sb2)
EndIf
UpdateWindowMenu win
End Select
End Select
Forever




;global functions
Function GetBit(byte, bitnumber)
ret = byte And 2 ^ bitnumber
If ret <> 0 Then ret = 1
Return ret
End Function

EDIT: Code aktualisiert.

Download Syntaxhighlighting

Die Strukturierung wird zwar schlecht sein, aber das ist meine erste Veröffentlichung...
Kritik erwünscht Wink (gerne auch per PM, wenn die Postgenehmigung hier abgelaufen ist...)

EDIT: Jetzt werden Scrollbalken ausgeblendet, wenn unnötig, daher also mein Tipp: Sachen, die vlt. gescrollt werden müssen, im Zweifelsfall gleich in eine ScrollBox schreiben. Sieht dann aus wie ein Panel normal.

(PS:) Ach ja, und ohne Pics saug ich nix:
user posted image
(Verzeiht mir die schlechte Bildqualität Embarassed )

Mit freundlichen Grüßen,
KnorxThieus
  • Zuletzt bearbeitet von KnorxThieus am Mo, März 03, 2014 10:14, insgesamt 2-mal bearbeitet

ozzi789

BeitragSo, Jun 09, 2013 10:25
Antworten mit Zitat
Benutzer-Profile anzeigen
Klappt bestens, kommt in den BB-Funktionen Ordner Smile
0x2B || ! 0x2B
C# | C++13 | Java 7 | PHP 5

KnorxThieus

BeitragSo, Jun 09, 2013 13:48
Antworten mit Zitat
Benutzer-Profile anzeigen
Fein, freut mich, dass jemand das mag! Very Happy Very Happy
(Den Screenshot habe ich aktualisiert.)

EDIT: Ach ja, und an Verbesserungsvorschlägen bin ich gern interessiert. Wink
Version: BlitzPlus / Blitz+
 

Dannny04

Betreff: Sehr praktischer Code

BeitragMo, Nov 11, 2013 1:16
Antworten mit Zitat
Benutzer-Profile anzeigen
Sieht sehr gut aus Smile werde es wahrscheinlich für mein nächstes Projekt gut gebrauchen können.

Mfg
Danny

KnorxThieus

BeitragMo, Nov 11, 2013 17:56
Antworten mit Zitat
Benutzer-Profile anzeigen
Hey, freut mich sehr! Und an Vorschlägen bin ich immer noch gerne interessiert!
MFG
Version: BlitzPlus / Blitz+

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group