jueves, 13 de julio de 2017

Une





Idea básica para armar un programa de este tipo.
Observalo en youtube:



' gambas class file

Public Cantidad_Sprites As Picture[]
Public XpictureBox As PictureBox
Public TagOrdenado As String[]
Public Repeticiones As Integer = 200
Public Intentos As Integer

Public PICactual As Picture
Public pbActual As PictureBox

Public Sub Form_Open()

Label1.Text = "by postapase v. " & Application.Version

ReiniciarVariables()

SepararImagenes("casa.jpeg", 3, 3)

End

Public Sub SepararImagenes(path As String, filas As Integer, columnas As Integer)
'subrutina extraida y modificada del ejemplo de Julio 1945

Dim i As Image
Dim fila As Integer
Dim columna As Integer
Dim imagenAncho As Integer
Dim imagenLargo As Integer
Dim fichero As Picture
Dim contador As Integer = 1
Dim ruta As String
Dim a As Integer

i = Image.Load(path)
PictureBox1.Visible = False
PictureBox1.Picture = Picture.Load(path)

Cantidad_Sprites = New Picture[]

imagenAncho = i.w / columnas
imagenlargo = i.h / filas

For fila = 1 To filas
For columna = 1 To columnas

fichero = New Picture(imagenAncho, imagenLargo, Color.Transparent)
Paint.Begin(fichero)

Paint.DrawImage(i, 0, 0, imagenAncho, imagenLargo, 1, Rect((columna - 1) * imagenAncho, (fila - 1) * imagenLargo, imagenancho, imagenlargo))
Paint.Stroke()
Paint.Fill
Paint.End()
Wait 0.1
ruta = "/tmp/dibujo" & Str$(contador) & ".png"
fichero.Save(ruta)
Wait 0.1
Cantidad_Sprites.Add(fichero)
TagOrdenado.Add(ruta)

Select Case contador
Case 1
pb1.Tag = ruta
Case 2
pb2.Tag = ruta
Case 3
pb3.Tag = ruta
Case 4
pb4.Tag = ruta
Case 5
pb5.Tag = ruta
Case 6
pb6.Tag = ruta
Case 7
pb7.Tag = ruta
Case 8
pb8.Tag = ruta
Case 9
pb9.Tag = ruta
End Select

contador += 1

Next
Next

For a = 1 To Repeticiones
Desordenar()
Next

Refrescar2()

End

Public Sub btnOtraImagen_Click()


Dialog.Path = User.Home
Dialog.Filter = ["*.png;*.jpg;*.jpeg;*.bmp", "Picture files"]

If Dialog.OpenFile() Then Return

Select Case File.Ext(Dialog.Path)
Case "png", "jpg", "jpeg", "bmp"
Case Else
Message.Error("Imagen no válida!")
Return
End Select

ReiniciarVariables()

SepararImagenes(Dialog.Path, 3, 3)

End

Public Sub parte2_Drag()

Print Last.Name
XpictureBox.Drag(XpictureBox.Picture)

End

Public Sub parte2_Drop()

Print Last.Name

End

Public Sub parte2_DblClick()

Print Last.Name

End

Public Sub pb9_MouseDrag()

Drag.Icon = pb9.Picture

If Mouse.Left Then
pb9.Drag(pb9.Tag)
Endif

End

Public Sub pb8_Drop()

Dim MiTag As String

MiTag = pb8.Tag

pb8.Picture = Picture.Load(Drag.Data)
pb8.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb6_MouseDrag()

Drag.Icon = pb6.Picture

If Mouse.Left Then
pb6.Drag(pb6.Tag)

Endif

End

Public Sub Refrescar()

pb1.Picture = Picture.Load(pb1.Tag)
pb2.Picture = Picture.Load(pb2.Tag)
pb3.Picture = Picture.Load(pb3.Tag)

Wait 0.2

pb4.Picture = Picture.Load(pb4.Tag)
pb5.Picture = Picture.Load(pb5.Tag)
pb6.Picture = Picture.Load(pb6.Tag)

Wait 0.2

pb7.Picture = Picture.Load(pb7.Tag)
pb8.Picture = Picture.Load(pb8.Tag)
pb9.Picture = Picture.Load(pb9.Tag)

If Intentos > 5 Then
btnSolucion.Visible = True
Endif

Inc Intentos

End

Public Sub Desordenar()

Dim max As Integer
Dim uno As Integer
Dim dos As Integer

Randomize
max = TagOrdenado.Max

uno = Rand(0, max)
dos = Rand(0, max)

Repeat
dos = Rand(0, max)
Until uno <> dos

Swap TagOrdenado[uno], TagOrdenado[dos]

End

Public Sub Refrescar2()

pb1.Tag = TagOrdenado[0]
pb2.Tag = TagOrdenado[1]
pb3.Tag = TagOrdenado[2]

pb4.Tag = TagOrdenado[3]
pb5.Tag = TagOrdenado[4]
pb6.Tag = TagOrdenado[5]

pb7.Tag = TagOrdenado[6]
pb8.Tag = TagOrdenado[7]
pb9.Tag = TagOrdenado[8]

pb1.Picture = Null
pb2.Picture = Null
pb3.Picture = Null
pb4.Picture = Null
pb5.Picture = Null
pb6.Picture = Null
pb7.Picture = Null
pb8.Picture = Null
pb9.Picture = Null

Wait 0.5

Refrescar()

End

Public Sub pb5_MouseDrag()

Drag.Icon = pb5.Picture

If Mouse.Left Then
pb5.Drag(pb5.Tag)

Endif

End

Public Sub pb9_Drop()

Dim MiTag As String

MiTag = pb9.Tag

pb9.Picture = Picture.Load(Drag.Data)
pb9.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb7_Drop()

Dim MiTag As String

MiTag = pb7.Tag

pb7.Picture = Picture.Load(Drag.Data)
pb7.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb6_Drop()

Dim MiTag As String

MiTag = pb6.Tag

pb6.Picture = Picture.Load(Drag.Data)
pb6.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb5_Drop()

Dim MiTag As String

MiTag = pb5.Tag

pb5.Picture = Picture.Load(Drag.Data)
pb5.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb7_MouseDrag()

Drag.Icon = pb7.Picture

If Mouse.Left Then
pb7.Drag(pb7.Tag)

Endif

End

Public Sub pb4_MouseDrag()

Drag.Icon = pb4.Picture

If Mouse.Left Then
pb4.Drag(pb4.Tag)

Endif

End

Public Sub pb8_MouseDrag()

Drag.Icon = pb8.Picture

If Mouse.Left Then
pb8.Drag(pb8.Tag)
Endif

End

Public Sub pb1_MouseDrag()

Drag.Icon = pb1.Picture

If Mouse.Left Then
pb1.Drag(pb1.Tag)

Endif

End

Public Sub pb2_MouseDrag()

Drag.Icon = pb2.Picture

If Mouse.Left Then
pb2.Drag(pb2.Tag)

Endif

End

Public Sub pb3_MouseDrag()

Drag.Icon = pb3.Picture

If Mouse.Left Then
pb3.Drag(pb3.Tag)

Endif

End

Public Sub pb4_Drop()

Dim MiTag As String

MiTag = pb4.Tag

pb4.Picture = Picture.Load(Drag.Data)
pb4.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb1_Drop()

Dim MiTag As String

MiTag = pb1.Tag

pb1.Picture = Picture.Load(Drag.Data)
pb1.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb2_Drop()

Dim MiTag As String

MiTag = pb2.Tag

pb2.Picture = Picture.Load(Drag.Data)
pb2.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub pb3_Drop()

Dim MiTag As String

MiTag = pb3.Tag

pb3.Picture = Picture.Load(Drag.Data)
pb3.Tag = Drag.Data

Drag.Source.Tag = MiTag
Refrescar()

End

Public Sub btnDesordenar_Click()

Desordenar()
Refrescar2

End

Public Sub ReiniciarVariables()

PBactual = New PictureBox(Me)
TagOrdenado = New String[]
Intentos = 0
btnSolucion.Visible = False

End

Public Sub btnSolucion_Click()

PictureBox1.Visible = Not PictureBox1.Visible

End

Public Sub cbxFotos_Click()

ReiniciarVariables()

SepararImagenes(cbxFotos.Text, 3, 3)

End



El ejemplo se encuentra en la granja de gambas.

viernes, 7 de julio de 2017

Control personalizado


Aquí les dejo mi experimento en la creación de una clase o control personalizado, a medida para un fin x. El código esta en la granja.


' gambas class file

Export

Inherits UserControl

Public Const _Properties As String = "*,Texto,Seleccion,Hombre=True"
Public Const _DefaultEvent As String = "Eleccion"
Public Const _DefaultSize As String = "36,4"

Event Eleccion

Property Seleccion As Boolean
Property Texto As String
Property Hombre As Boolean

Private $Seleccion As Boolean
Private $Texto As String
Private $Hombre As Boolean

Private $hFondo As DrawingArea
Private $TextLabel As TextLabel
Private $CheckBox As CheckBox
Private $Persona As PictureBox

Public Sub _new()

$hFondo = New DrawingArea(Me) As "Fondo"
$hFondo.Background = &HFFEFBF
$hFondo.Arrangement = Arrange.Horizontal

$CheckBox = New CheckBox($hFondo) As "Check"
$CheckBox.Width = 20
$CheckBox.AutoResize = True
$CheckBox.Background = &HD2DFC3

$TextLabel = New TextLabel($hFondo) As "TexLabel"
$TextLabel.Alignment = Align.Center
$TextLabel.Expand = True
$TextLabel.Background = &HFFEFBF

$Persona = New PictureBox($hFondo) As "Persona"
Hombre_Write(True)
$Persona.Width = 40
$Persona.Stretch = True
$Persona.AutoResize = True

End

Public Sub Fondo_Enter()

$hFondo.Background = &H00FFFF

End

Public Sub Fondo_Leave()

$hFondo.Background = &HFFEFBF

End


Public Sub TexLabel_Enter()

$TextLabel.Tooltip = Me.Name

End

Public Sub TexLabel_MouseUp()

$CheckBox.Value = Not $CheckBox.Value

End

Public Sub Persona_MouseUp()

$CheckBox.Value = Not $CheckBox.Value

End


Public Sub Check_Click()

$Seleccion = $CheckBox.Value
Raise Eleccion

End

Private Function Seleccion_Read() As Boolean

Return $Seleccion

End

Private Sub Seleccion_Write(Value As Boolean)

$Seleccion = Value

End

Private Function Texto_Read() As String

$Texto = $TextLabel.Text
Return $Texto

End

Private Sub Texto_Write(Value As String)

$Texto = Value
$TextLabel.Text = $Texto

End

Private Function Hombre_Read() As Boolean

Return $Hombre

End

Private Sub Hombre_Write(Value As Boolean)

$Hombre = Value

If $Hombre Then
$Persona.Picture = Picture.Load("hombre.png")
Else
$Persona.Picture = Picture.Load("mujer.png")
Endif

End


lunes, 3 de julio de 2017

Uso de spriters en Gambas




Aprendiendo a usar spriters en gambas, empiezo con algo simple.

' gambas class file

' Como usar una imagen sprite en gambas by postapase (3 Julio 2017)

Private Cantidad_Sprites As New Picture[]

Public Sub Form_Open()

Me.Title = "Sprite"

End

Public Sub SepararImagenes(path As String, filas As Integer, columnas As Integer)
'subrutina extraida y modificada del ejemplo de Julio 1945

Dim i As Image
Dim fila As Integer
Dim columna As Integer
Dim imagenAncho As Integer
Dim imagenLargo As Integer
Dim fichero As Picture
Dim contador As Integer

i = Image.Load(path)

Cantidad_Sprites = New Picture[]

imagenAncho = i.w / columnas
imagenlargo = i.h / filas

For fila = 1 To filas
For columna = 0 To columnas

fichero = New Picture(imagenAncho, imagenLargo, Color.Transparent)
Paint.Begin(fichero)


'esta línea lo que hace es recortar la imagen i,
 y ese recorte sera la nueva imagen.
Paint.DrawImage(i, 0, 0, imagenAncho, imagenLargo, 1, Rect((columna - 1) * imagenAncho, (fila - 1) * imagenLargo, imagenancho, imagenlargo))
Paint.Stroke()
Paint.Fill

Paint.End()
fichero.Save("/tmp/dibujo" & Str$(contador) & ".png")
Cantidad_Sprites.Add(fichero)
contador += 1

Next
Next

End

Private Sub Accion()

Dim a As Integer

For a = 0 To Cantidad_Sprites.Max
PictureBox1.Picture = Cantidad_Sprites[a]
Wait 0.09
Next

PictureBox1.Picture = Null
Wait 0.1

End

Public Sub Button1_Click()

SepararImagenes("explosion.png", 1, 12)
Accion()

End

El ejemplo esta disponible en la granja de gambas.

Página para descargar spriters y hacer nuestras experiencias en gambas.
https://www.spriters-resource.com/

www.gambas-es.org

www.gambas-es.org
Si quieres que tu curva de aprendizaje sea rápida, regístrate en este gran foro en español.