[B2D] Scrollbox für Images

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

SpionAtom

Betreff: [B2D] Scrollbox für Images

BeitragSo, Dez 25, 2011 15:48
Antworten mit Zitat
Benutzer-Profile anzeigen
Mit dieser Scrollbox ist es möglich, größere Bilder in einem kleinen Fenster anzeigen zu lassen. Mittels Scrollleiste kann der Bildausschnitt mit der Maus verändert werden:

BlitzBasic: [AUSKLAPPEN]
; SpionAtom - Dezember 2011
Graphics 800, 600, 0, 2
SetBuffer BackBuffer()

Global mouse_x, mouse_y

Type Scrollbox
Field pic, pic_width, pic_height
Field pos_x, pos_y
Field width, height
Field cR, cG, cB
Field h_scroll, v_scroll
Field bar, v_scroller, h_scroller, v_scroller_pos, h_scroller_pos
Field dragStarted_v, dragStarted_h, o_scroll, dragValue
End Type

S1.Scrollbox = createScrollbox(LoadImage("bild.bmp"), 10, 10, 300, 250, 15)
S2.Scrollbox = createScrollbox(LoadImage("bild.bmp"), 50, 300, 600, 250, 30, 255, 255, 0)

;Main-Loop
Repeat

mouse_x = MouseX()
mouse_y = MouseY()
Cls
doScrollbox S1
doScrollbox S2
Flip()

Until KeyDown(1)
End



;image - That image you want to display
;pos_x, pos_y - Position of your Scrollbox
;width, height - Size of your Scrollbox
;bar - Size of the Scrollbar(s)
;[r, g, b] - Color of your Scrollbox
Function createScrollbox.Scrollbox(image, pos_x, pos_y, width, height, bar, r = 155, g = 155, b = 155)

;Create new Scrollbox
;insert your own image, arrange position, size and color
S.Scrollbox = New Scrollbox
S\pic = image
S\pic_width = ImageWidth(S\pic)
S\pic_height = ImageHeight(S\pic)
S\width = width
S\height = height
S\cR = r: S\cG = g: S\cB = b
S\pos_x = pos_x
S\pos_y = pos_y
S\bar = bar
S\v_scroll = 0
S\h_scroll = 0
S\h_scroller = (S\width - (2.0 * S\bar)) / S\pic_width * (S\width - (2 * S\bar)): If S\h_scroller < S\bar Then S\h_scroller = S\bar
S\v_scroller = (S\height - (2.0 * S\bar)) / S\pic_height * (S\height - (2 * S\bar)): If S\v_scroller < S\bar Then S\v_scroller = S\bar
S\dragStarted_v = False
S\dragStarted_h = False

Return S

End Function

;S - Scrollbox that you want to display
Function doScrollbox(S.Scrollbox)


DrawImageRect S\pic, S\pos_x, S\pos_y, S\h_scroll, S\v_scroll, S\width, S\height

Color S\cR, S\cG, S\cB
Rect S\pos_x, S\pos_y, S\width, S\height, 0 ;Border


If S\pic_height > S\height Then

Color S\cR, S\cG, S\cB
Rect S\pos_x + S\width, S\pos_y, S\bar, S\height, 0 ;vertical bar
fill = False
If mouseInRect(S\pos_x + S\width, S\pos_y + S\bar + S\v_scroller_pos, S\bar, S\v_scroller) Then
fill = True
If Not S\dragStarted_v And MouseDown(1) Then
S\dragStarted_v = True
S\dragValue = mouse_y
S\o_scroll = S\v_scroller_pos
End If
End If
If S\dragStarted_v And MouseDown(1) Then
fill = True
S\v_scroller_pos = S\o_scroll + (mouse_y - S\dragValue)
If S\v_scroller_pos < 0 Then S\v_scroller_pos = 0
If S\v_scroller_pos > S\height - 2 * S\bar - S\v_scroller Then S\v_scroller_pos = S\height - 2 * S\bar - S\v_scroller
S\v_scroll = S\v_scroller_pos / (S\height - 2.0 * S\bar - S\v_scroller) * (S\pic_height - S\height) ;calculating position
End If
If Not MouseDown(1) And S\dragStarted_v Then
S\dragStarted_v = False
End If
Rect S\pos_x + S\width + 2, S\pos_y + S\bar + S\v_scroller_pos, S\bar - 4, S\v_scroller, fill ;vertical scroller


If ArrowBox(S\pos_x + S\width, S\pos_y, S\bar, S\bar, "UP", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\v_scroll > 0 Then S\v_scroll = S\v_scroll - 1
S\v_scroller_pos = (S\v_scroll / Float(S\pic_height - S\height)) * (S\height - 2 * S\bar - S\v_scroller)
End If
If ArrowBox(S\pos_x + S\width, S\pos_y + S\height - S\bar, S\bar, S\bar, "DOWN", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\v_scroll < S\pic_height - S\height Then S\v_scroll = S\v_scroll + 1
S\v_scroller_pos = (S\v_scroll / Float(S\pic_height - S\height)) * (S\height - 2 * S\bar - S\v_scroller)
End If


End If


If S\pic_width > S\width Then

Color S\cR, S\cG, S\cB
Rect S\pos_x, S\pos_y + S\height, S\width, S\bar, 0 ;horizontal bar
fill = False
If mouseInRect(S\pos_x + S\bar + S\h_scroller_pos, S\pos_y + S\height, S\h_scroller, S\bar) Then
fill = True
If Not S\dragStarted_h And MouseDown(1) Then
S\dragStarted_h = True
S\dragValue = mouse_x
S\o_scroll = S\h_scroller_pos
End If
End If
If S\dragStarted_h And MouseDown(1) Then
fill = True
S\h_scroller_pos = S\o_scroll + (mouse_x - S\dragValue)
If S\h_scroller_pos < 0 Then S\h_scroller_pos = 0
If S\h_scroller_pos > S\width - 2 * S\bar - S\h_scroller Then S\h_scroller_pos = S\width - 2 * S\bar - S\h_scroller
S\h_scroll = S\h_scroller_pos / (S\width - 2.0 * S\bar - S\h_scroller) * (S\pic_width - S\width) ;calculating position
End If
If Not MouseDown(1) And S\dragStarted_h Then
S\dragStarted_h = False
End If
Rect S\pos_x + S\bar + S\h_scroller_pos, S\pos_y + S\height + 2, S\h_scroller, S\bar - 4, fill ;horizontal scroller

If ArrowBox(S\pos_x, S\pos_y + S\height, S\bar, S\bar, "LEFT", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\h_scroll > 0 Then S\h_scroll = S\h_scroll - 1
S\h_scroller_pos = (S\h_scroll / Float(S\pic_width - S\width)) * (S\width - 2 * S\bar - S\h_scroller)
End If
If ArrowBox(S\pos_x + S\width - S\bar, S\pos_y + S\height, S\bar, S\bar, "RIGHT", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\h_scroll < S\pic_width - S\width Then S\h_scroll = S\h_scroll + 1
S\h_scroller_pos = (S\h_scroll / Float(S\pic_width - S\width)) * (S\width - 2 * S\bar - S\h_scroller)
End If

End If





End Function

Function ArrowBox(x, y, w, h, dir$, noHighlight = True, r = 55, g = 155, b = 155)

Color r, g, b
mouseIn = False
If mouseInRect(x, y, w, h) And noHighlight = False Then
Rect x, y, w, h, 1
Color 0, 0, 0
mouseIn = True
Else
Rect x, y, w, h, 0
Color r, g, b
End If

Select Upper(dir$)
Case "UP"
Line x + 0.5 * w, y + 0.25 * h, x + 0.25 * w, y + 0.75 * h
Line x + 0.5 * w, y + 0.25 * h, x + 0.75 * w, y + 0.75 * h
Case "DOWN"
Line x + 0.5 * w, y + 0.75 * h, x + 0.25 * w, y + 0.25 * h
Line x + 0.5 * w, y + 0.75 * h, x + 0.75 * w, y + 0.25 * h
Case "LEFT"
Line x + 0.25 * w, y + 0.5 * h, x + 0.75 * w, y + 0.25 * h
Line x + 0.25 * w, y + 0.5 * h, x + 0.75 * w, y + 0.75 * h
Case "RIGHT"
Line x + 0.75 * w, y + 0.5 * h, x + 0.25 * w, y + 0.25 * h
Line x + 0.75 * w, y + 0.5 * h, x + 0.25 * w, y + 0.75 * h
End Select

Return mouseIn And noHighlight = False

End Function

;Prüft, ob sich die Maus in einem angegebenen Rechteck befindet
Function mouseInRect(x, y, w, h)
If mouse_x <= x Then Return False
If mouse_y <= y Then Return False
If mouse_x >= x + w Then Return False
If mouse_y >= y + h Then Return False
Return True
End Function


EDIT:
Danke Darth, hab in meiner Lösung nun auch einen Konstruktor drin, aber so viel Arbeit wie du hab ich nicht mehr reininvestiert... Dafür ist es ja hier das Codearchiv, darf jeder dran rumwerkeln. Hab aber nun die Abfrage drin, dass die Scrollbalken nur angezeigt werden, wenn das Bild auch tatsächlich größer ist, als die Box.
os: Windows 10 Home cpu: Intel Core i7 6700K 4.00Ghz gpu: NVIDIA GeForce GTX 1080
  • Zuletzt bearbeitet von SpionAtom am Mo, Dez 26, 2011 21:47, insgesamt 5-mal bearbeitet

darth

BeitragSo, Dez 25, 2011 15:58
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo,

weil ichs so hässlich fand, habe ich das Erstellen der Scrollbar in eine Funktion abgepackt, dass man den Mist nicht ständig kopieren muss, wenn man ein zweites Bild mit Scroll möchte..

BlitzBasic: [AUSKLAPPEN]
; SpionAtom - Dezember 2011

;<!-- Test Program
Graphics 800, 600, 0, 2
SetBuffer BackBuffer()

Local S.Scrollbox = newScrollbox("Bild.bmp", 300, 250, (GraphicsWidth() - 300) / 2, (GraphicsHeight() - 250) / 2)

Repeat
mouse_x = MouseX()
mouse_y = MouseY()
Cls
doScrollbox S
Flip()
Until KeyDown(1)
End
;Test Program --!>

;<!--- LIB AB HIER ---!>

Global mouse_x, mouse_y

Type Scrollbox
Field pic, pic_width, pic_height
Field pos_x, pos_y
Field width, height
Field cR, cG, cB
Field h_scroll, v_scroll
Field bar, v_scroller, h_scroller, v_scroller_pos, h_scroller_pos
Field dragStarted_v, dragStarted_h, o_scroll, dragValue
End Type

Function newScrollbox.Scrollbox(path$, width, height)
Local S.Scrollbox = New Scrollbox

S\pic = LoadImage(path)
S\pic_width = ImageWidth(S\pic)
S\pic_height = ImageHeight(S\pic)

S\width = width
S\height = height

S\cR = 55
S\cG = 155
S\cB = 155

S\pos_x = 0
S\pos_y = 0

S\bar = 15
S\v_scroll = 0
S\h_scroll = 0
S\h_scroller = (S\width - (2.0 * S\bar)) / S\pic_width * (S\width - (2 * S\bar)): If S\h_scroller < S\bar Then S\h_scroller = S\bar
S\v_scroller = (S\height - (2.0 * S\bar)) / S\pic_height * (S\height - (2 * S\bar)): If S\v_scroller < S\bar Then S\v_scroller = S\bar
S\dragStarted_v = False
S\dragStarted_h = False

Return S
End Function

Function setPosition(s.Scrollbox, posX, posY)
s\pos_x = posX
s\pos_y = posY
End Function

Function setColor(s.Scrollbox, red, grn, blu)
s\cR = red
s\cG = grn
s\cB = blu
End Function

Function doScrollbox(S.Scrollbox)
DrawImageRect S\pic, S\pos_x, S\pos_y, S\h_scroll, S\v_scroll, S\width, S\height

Color S\cR, S\cG, S\cB
Rect S\pos_x, S\pos_y, S\width, S\height, 0 ;Border

Rect S\pos_x + S\width, S\pos_y, S\bar, S\height, 0 ;vertical bar
fill = False
If mouseInRect(S\pos_x + S\width, S\pos_y + S\bar + S\v_scroller_pos, S\bar, S\v_scroller) Then
fill = True
If Not S\dragStarted_v And MouseDown(1) Then
S\dragStarted_v = True
S\dragValue = mouse_y
S\o_scroll = S\v_scroller_pos
End If
End If
If S\dragStarted_v And MouseDown(1) Then
fill = True
S\v_scroller_pos = S\o_scroll + (mouse_y - S\dragValue)
If S\v_scroller_pos < 0 Then S\v_scroller_pos = 0
If S\v_scroller_pos > S\height - 2 * S\bar - S\v_scroller Then S\v_scroller_pos = S\height - 2 * S\bar - S\v_scroller
S\v_scroll = S\v_scroller_pos / (S\height - 2.0 * S\bar - S\v_scroller) * (S\pic_height - S\height) ;calculating position
End If
If Not MouseDown(1) And S\dragStarted_v Then
S\dragStarted_v = False
End If
Rect S\pos_x + S\width + 2, S\pos_y + S\bar + S\v_scroller_pos, S\bar - 4, S\v_scroller, fill ;vertical scroller

Rect S\pos_x, S\pos_y + S\height, S\width, S\bar, 0 ;horizontal bar
fill = False
If mouseInRect(S\pos_x + S\bar + S\h_scroller_pos, S\pos_y + S\height, S\h_scroller, S\bar) Then
fill = True
If Not S\dragStarted_h And MouseDown(1) Then
S\dragStarted_h = True
S\dragValue = mouse_x
S\o_scroll = S\h_scroller_pos
End If
End If
If S\dragStarted_h And MouseDown(1) Then
fill = True
S\h_scroller_pos = S\o_scroll + (mouse_x - S\dragValue)
If S\h_scroller_pos < 0 Then S\h_scroller_pos = 0
If S\h_scroller_pos > S\width - 2 * S\bar - S\h_scroller Then S\h_scroller_pos = S\width - 2 * S\bar - S\h_scroller
S\h_scroll = S\h_scroller_pos / (S\width - 2.0 * S\bar - S\h_scroller) * (S\pic_width - S\width) ;calculating position
End If
If Not MouseDown(1) And S\dragStarted_h Then
S\dragStarted_h = False
End If
Rect S\pos_x + S\bar + S\h_scroller_pos, S\pos_y + S\height + 2, S\h_scroller, S\bar - 4, fill ;horizontal scroller

If ArrowBox(S\pos_x + S\width, S\pos_y, S\bar, S\bar, "UP", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\v_scroll > 0 Then S\v_scroll = S\v_scroll - 1
S\v_scroller_pos = (S\v_scroll / Float(S\pic_height - S\height)) * (S\height - 2 * S\bar - S\v_scroller)
End If
If ArrowBox(S\pos_x + S\width, S\pos_y + S\height - S\bar, S\bar, S\bar, "DOWN", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\v_scroll < S\pic_height - S\height Then S\v_scroll = S\v_scroll + 1
S\v_scroller_pos = (S\v_scroll / Float(S\pic_height - S\height)) * (S\height - 2 * S\bar - S\v_scroller)
End If
If ArrowBox(S\pos_x, S\pos_y + S\height, S\bar, S\bar, "LEFT", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\h_scroll > 0 Then S\h_scroll = S\h_scroll - 1
S\h_scroller_pos = (S\h_scroll / Float(S\pic_width - S\width)) * (S\width - 2 * S\bar - S\h_scroller)
End If
If ArrowBox(S\pos_x + S\width - S\bar, S\pos_y + S\height, S\bar, S\bar, "RIGHT", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then
If S\h_scroll < S\pic_width - S\width Then S\h_scroll = S\h_scroll + 1
S\h_scroller_pos = (S\h_scroll / Float(S\pic_width - S\width)) * (S\width - 2 * S\bar - S\h_scroller)
End If
End Function

Function ArrowBox(x, y, w, h, dir$, noHighlight = True, r = 55, g = 155, b = 155)
Color r, g, b
mouseIn = False
If mouseInRect(x, y, w, h) And noHighlight = False Then
Rect x, y, w, h, 1
Color 0, 0, 0
mouseIn = True
Else
Rect x, y, w, h, 0
Color r, g, b
End If

Select Upper(dir$)
Case "UP"
Line x + 0.5 * w, y + 0.25 * h, x + 0.25 * w, y + 0.75 * h
Line x + 0.5 * w, y + 0.25 * h, x + 0.75 * w, y + 0.75 * h
Case "DOWN"
Line x + 0.5 * w, y + 0.75 * h, x + 0.25 * w, y + 0.25 * h
Line x + 0.5 * w, y + 0.75 * h, x + 0.75 * w, y + 0.25 * h
Case "LEFT"
Line x + 0.25 * w, y + 0.5 * h, x + 0.75 * w, y + 0.25 * h
Line x + 0.25 * w, y + 0.5 * h, x + 0.75 * w, y + 0.75 * h
Case "RIGHT"
Line x + 0.75 * w, y + 0.5 * h, x + 0.25 * w, y + 0.25 * h
Line x + 0.75 * w, y + 0.5 * h, x + 0.25 * w, y + 0.75 * h
End Select

Return mouseIn And noHighlight = False
End Function

;Prüft, ob sich die Maus in einem angegebenen Rechteck befindet
Function mouseInRect(x, y, w, h)
If mouse_x <= x Then Return False
If mouse_y <= y Then Return False
If mouse_x >= x + w Then Return False
If mouse_y >= y + h Then Return False
Return True
End Function


Edit: Habs noch ein wenig nachgebessert. Damit der Constructor nicht derart länglich ist, habe ich alles rausgenommen und in Setter abgepackt. So wie es uns in der OOP-Vorlesung gelehrt wurde.
Hoffe man nimmt mir die stilistische Änderung fremnden Codes nicht übel.
MfG,
Darth
  • Zuletzt bearbeitet von darth am Mo, Dez 26, 2011 2:32, insgesamt einmal bearbeitet

ozzi789

BeitragSo, Dez 25, 2011 18:52
Antworten mit Zitat
Benutzer-Profile anzeigen
Kommt in den Code Ordner, thx! Smile
0x2B || ! 0x2B
C# | C++13 | Java 7 | PHP 5

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group