So, dann mache ich mal den Anfang, folgender Code funktioniert zwar, aber so ganz zufrieden bin ich damit noch nicht.
Es werden 3 Slider für die Farben rot, grün und blau erstellt, diese lassen sich per Maus verschieben.
Wenn ein Slider verändert wird, wird die veränderte Farbe in einem Array geändert und in der Hauptschleife wird der neue CLS-Wert eingestellt.
Beim laden wird die datei color.cfg gesucht, ist diese nicht vorhanden, werden alle werte auf 128 voreingestellt, beim schließen werden die Werte in die vorgenannte Datei gespeichert.
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN]
SuperStrict
Local width:Int = 800 Local height:Int = 600 Local timer:TTimer = CreateTimer(60)
Local quit:Byte = False Local index:Int = False Local colors:String[] = ["red", "green", "blue"] Local color:Int[] = [128, 128, 128] Local path:String = "color.cfg"
Local kh_esc:Byte Local kh_spc:Byte Local kh_ret:Byte
Local mx:Int, my:Int Local mh1:Byte, md1:Byte
Graphics width, height
For Local i:Int = 0 To 2 TSlider.Create(colors[i], 5, 5+(i*30)) Next
Local file:TStream = ReadStream(path)
If file Then For Local slider:TSlider = EachIn TSlider._list For Local i:Int = 0 To 2 If colors[i] = slider._name Then color[i] = ReadInt(file) slider._value = color[i] EndIf Next Next CloseFile(file) EndIf
While Not quit Local ticks:Int = WaitTimer(timer) kh_esc = KeyHit(KEY_ESCAPE) kh_spc = KeyHit(KEY_SPACE) mx = MouseX() my = MouseY() mh1 = MouseHit(1) md1 = MouseDown(1) If kh_esc Or AppTerminate() Then quit = True If mh1 Or md1 Then For Local slider:TSlider = EachIn TSlider._list If (slider._x < mx) And (mx < (slider._x+slider._w)) Then If (slider._y < my) And (my < (slider._y+slider._h)) Then Local value:Int = mx - slider._x slider.Update(value) For Local i:Int = 0 To 2 If slider._name = colors[i] Then color[i] = slider._value EndIf Next EndIf EndIf Next EndIf SetClsColor(color[0], color[1], color[2]) TSlider.Draw() Flip 0 Cls Wend
SaveColor(path, color)
TSlider.DeleteAll()
Function LoadColor:Int[](path:String) Local file:TStream = ReadStream(path) Local color:Int[3] If file Then color[0] = ReadInt(file) color[1] = ReadInt(file) color[2] = ReadInt(file) CloseStream(file) Return color Else EndIf End Function
Function SaveColor(path:String, rgb:Int[]) Local file:TStream = WriteStream(path) If Not file Then Notify("Datei '" + path+ "' konnte nicht erstellt werden") Else WriteInt file, rgb[0] WriteInt file, rgb[1] WriteInt file, rgb[2] CloseStream(file) EndIf End Function
Type TSlider Const _range:Int = 256 Global _list:TList = CreateList() Field _link:TLink Field _x:Int Field _y:Int Field _w:Int Field _h:Int Field _name:String Field _value:Int
Method New() _link = _list.addlast(Self) End Method Method Destroy() _link.Remove() End Method Method Update(value:Int) If value < _range Then _value = value EndIf End Method Function Create:TSlider(name:String, x:Int, y:Int, w:Int=255, h:Int=25) Local slider:TSlider = New TSlider slider._x = x slider._y = y slider._w = w slider._h = h slider._name = name slider._value = 128 Return slider End Function
Function Draw:Int() For Local slider:TSlider = EachIn TSlider._list SetColor(128, 128, 128) DrawRect slider._x-1, slider._y-1, slider._w+2, slider._h+2 SetColor(GetColor(slider._name, "red"), GetColor(slider._name, "green"), GetColor(slider._name, "blue")) DrawRect slider._x, slider._y, slider._value, slider._h SetColor(255, 255, 255) DrawText slider._value, (slider._x+(slider._w/2)-TextWidth(slider._value)/2), (slider._y+(slider._h/2)-TextHeight(slider._value)/2) Next End Function Function DeleteAll:Int() For Local slider:Tslider = EachIn TSlider._list slider.Destroy() Next End Function End Type
Function GetColor:Int(color:String, id:String) Select color Case "red" Select id Case "red" Return 255 End Select Case "green" Select id Case "green" Return 255 End Select Case "blue" Select id Case "blue" Return 255 End Select End Select Return 0 End Function
Ich weiß, dass es nicht das Nonplusultra ist, aber es funktioniert, ich bin auch noch an einer Version dran, wo die Types direkt in einem Array gespeichert werden, um es noch ein wenig zu optimieren, aber aus Zeitmangel weiß ich noch nicht, ob es fertig wird.
Zudem habe ich das Problem, dass ich ein Array von 0-2 (rgb[i]) erstelle, aber wenn ich dann aus die einzelnen Array Einträge zugreifen will(SetClsColor(rgb[0]._value, etc)) wird mir gesagt, dass ich versuche auf ein Feld oder eine Methode eines nicht vorhandenen Objektes zugreifen möchte.
Falls mir dazu jemand einen Tipp per PN, nicht unbedingt hier in diesem Thema, es sei den es ist erwünscht, schreiben möchte, wäre ich sehr dankbar.
[Edit]
Das Array ist als rgb[3]:TSlider initalisiert, falls das weiter hilft
[Edit2]
Ahh ok, hat sich erledigt, man sollte auch beim erstellen das Objekt zurückgeben, typischer Anfängerfehler
[Edit3]
Und hier noch mal Objektbasiert
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN]
SuperStrict
Local width:Int = 800 Local height:Int = 600 AppTitle = "Hintergrundfarbe - speichern/laden" Local quit:Byte = False Local timer:TTimer = CreateTimer(60) Local color:String[] = ["red", "green", "blue"] Local rgb:TSlider[3] Local path:String = "color.cfg" Local kh_esc:Byte Local kh_spc:Byte Local mx:Int, my:Int Local md1:Int
Type TSlider Const _range:Int = 256 Global _list:TList = CreateList() Field _link:TLink Field _name:String Field _x:Int, _y:Int Field _w:Int, _h:Int Field _value:Int = 128 Method New() _link = _list.AddLast(Self) End Method Method Destroy() _link.Remove() End Method Function Create:TSlider(name:String, x:Int, y:Int, color:Int=128, w:Int=255, h:Int=25) Local slider:TSlider = New TSlider slider._name = name slider._x = x slider._y = y slider._w = w slider._h = h slider._value = color Return slider End Function Function Update:TSlider(mx:Int, my:Int) For Local slider:TSlider = EachIn TSlider._list If (slider._y < my) And (my < (slider._y+slider._h)) Then If (slider._x-1 < mx) And (mx < (slider._x+slider._w+1)) Then slider._value = mx-slider._x EndIf EndIf Next End Function Function Draw() For Local slider:TSlider = EachIn TSlider._list Local half:Int = slider._range/2 SetColor(half, half, half) DrawRect slider._x-1, slider._y-1, slider._w+2, slider._h+2 SetColor (GetColor(slider._name, "red"), GetColor(slider._name, "green"), GetColor(slider._name, "blue")) DrawRect slider._x, slider._y, slider._value, slider._h SetColor(255, 255, 255) DrawText slider._value, (slider._x+(slider._w/2)-TextWidth(slider._value)/2), (slider._y+(slider._h/2)-TextHeight(slider._value)/2) Next End Function Function Save(path:String) Local file:TStream = WriteStream(path) If Not file Then Notify("Datei '" + path + "' konnte nicht erstellt werden") Else For Local slider:TSlider = EachIn TSlider._list WriteInt(file, slider._value) Next CloseStream(file) EndIf End Function End Type
Graphics width, height
Local file:TStream = ReadStream(path) If file Then For Local i:Int = 0 To 2 rgb[i] = TSlider.Create(color[i], 5, 5+(i*30), ReadInt(file)) Next CloseStream(file) Else For Local i:Int = 0 To 2 rgb[i] = TSlider.Create(color[i], 5, 5+(i*30)) Next EndIf
While Not quit Local ticks:Int = WaitTimer(timer) kh_esc = KeyHit(KEY_ESCAPE) mx = MouseX() my = MouseY() md1 = MouseDown(1) If kh_esc Or AppTerminate() Then quit = True If md1 Then TSlider.Update(mx, my) SetClsColor(rgb[0]._value, rgb[1]._value, rgb[2]._value) TSlider.Draw() Flip 0 Cls Wend
TSlider.Save(path)
End
Function GetColor:Int(color:String, id:String) Select color Case "red" Select id Case "red" Return 255 End Select Case "green" Select id Case "green" Return 255 End Select Case "blue" Select id Case "blue" Return 255 End Select End Select Return 0 End Function
|