~xigoi/vier

e7f4bf6db003678aae5c58e08485c65553fa809d — Adam Blažek 8 months ago 9a6f34d
Add mode
2 files changed, 38 insertions(+), 7 deletions(-)

M src/palettes.nim
M src/vier.nim
M src/palettes.nim => src/palettes.nim +3 -3
@@ 24,9 24,9 @@ let
      [
        color"#FF0055",
        color"#00000000",
        color"#00000000",
        color"#00000000",
        color"#00000000",
        color"#00000066",
        color"#000000AA",
        color"#000000FF",
        color"#00FF00"
      ],
      [

M src/vier.nim => src/vier.nim +35 -4
@@ 19,7 19,7 @@ import std/unicode
type
  Mode = enum
    mInject = "Inject"
    # mAdd = "Add"
    mAdd = "Add"
    mSelect = "Select"
    mColor = "Color"
    mCommand = "Command"


@@ 68,7 68,7 @@ type
    lastWritePos: int

  App = ref object
    spry: spryvm.Interpreter
    spry: spryvm.Interpreter # Interpreter of the Spry language
    font: Font
    palettes: seq[Picture]
    pictures: seq[Picture]


@@ 77,7 77,7 @@ type
    color: Color
    secondaryColor: Color = color"#00000000"
    secondaryColorPos: tuple[palette: int, cursor: Vec] = (0, (1, 1))
    movementTime: int
    movementTime: int # Time before the cursor can be moved
    command: string
    mode: Mode
    tool: Tool


@@ 104,6 104,7 @@ var toolIcons: array[ToolKind, Texture]
proc loadIcons() =
  modeIcons = [
    mInject: loadTextureStatic("icons/modes/inject.png"),
    mAdd: loadTextureStatic("icons/modes/add.png"),
    mSelect: loadTextureStatic("icons/modes/select.png"),
    mColor: loadTextureStatic("icons/modes/color.png"),
    mCommand: loadTextureStatic("icons/modes/command.png")


@@ 157,6 158,7 @@ proc widgetSize(picture: Picture, isPalette = false): Vec =
    picture.canvasSize + (0'i32, margin + textSize)

proc moveCursor(picture: Picture, movement: Vec) =
  ## Moves the cursor by the given amount, ensuring that it stays in bounds.
  picture.cursor.x = clamp(picture.cursor.x + movement.x, 0'i32..<picture.image.width)
  picture.cursor.y = clamp(picture.cursor.y + movement.y, 0'i32..<picture.image.height)



@@ 278,6 280,7 @@ proc select(picture: Picture, target: Vec, tool: Tool) =
    picture.selection = lineSegment(picture.anchor, target, includeStart = true)

proc injectColor(picture: Picture, color: Color) =
  ## Changes all selected pixels to the given color.
  let
    change =
      ImageChange(


@@ 294,6 297,25 @@ proc injectColor(picture: Picture, color: Color) =
  picture.add(change)
  picture.apply(change)

proc addColor(picture: Picture, color: Color) =
  ## Adds the given color to all selected pixels.
  let
    change =
      ImageChange(
        pixelChanges:
          collect(
            for pixel in picture.selection:
              let originalColor = picture.image.getImageColor(pixel.x, pixel.y)
              PixelChange(
                pos: pixel,
                originalColor: originalColor,
                newColor: colorAlphaBlend(originalColor, color, White),
              )
          )
      )
  picture.add(change)
  picture.apply(change)

proc clickPixel(picture: Picture, target: Vec, mode: Mode, tool: Tool, color: Color) =
  ## Reacts to clicking the image at the given point or pressing spacebar with the cursor on it.
  picture.selection = @[]


@@ 302,6 324,9 @@ proc clickPixel(picture: Picture, target: Vec, mode: Mode, tool: Tool, color: Co
  of mInject:
    picture.select(target, tool)
    picture.injectColor(color)
  of mAdd:
    picture.select(target, tool)
    picture.addColor(color)
  of mSelect:
    picture.select(target, tool)
  of mColor, mCommand:


@@ 315,6 340,10 @@ proc dragPixel(picture: Picture, target: Vec, mode: Mode, tool: Tool, color: Col
    picture.select(target, tool)
    picture.undo()
    picture.injectColor(color)
  of mAdd:
    picture.select(target, tool)
    picture.undo()
    picture.addColor(color)
  of mSelect:
    picture.select(target, tool)
  of mColor, mCommand:


@@ 563,6 592,8 @@ proc processKeyboard(app: App) =
      while (var ch = getCharPressed(); ch != 0):
        app.command.add(Rune(ch))
  else:
    if isKeyPressed(A):
      app.mode = mAdd
    if isKeyPressed(B):
      app.tool = Tool(kind: tBrush)
    if isKeyPressed(C):


@@ 620,7 651,7 @@ proc processKeyboard(app: App) =
        movement = square(0)
      app.movementTime.inc
    case app.mode
    of mInject, mSelect:
    of mInject, mAdd, mSelect:
      if movement != square(0):
        if control:
          app.selectedPictureIndex =