doodle/pkg/wallpaper/wallpaper_test.go

112 lines
2.5 KiB
Go
Raw Permalink Normal View History

Wallpapers and Bounded Levels Implement the Wallpaper system into the levels and the concept of Bounded and Unbounded levels. The first wallpaper image is notepad.png which looks like standard ruled notebook paper. On bounded levels, the top/left edges of the page look as you would expect and the blue lines tile indefinitely in the positive directions. On unbounded levels, you only get the repeating blue lines but not the edge pieces. A wallpaper is just a rectangular image file. The image is divided into four equal quadrants to be the Corner, Top, Left and Repeat textures for the wallpaper. The Repeat texture is ALWAYS used and fills all the empty space behind the drawing. (Doodads draw with blank canvases as before because only levels have wallpapers!) Levels have four options of a "Page Type": - Unbounded (default, infinite space) - NoNegativeSpace (has a top left edge but can grow infinitely) - Bounded (has a top left edge and bounded size) - Bordered (bounded with bordered texture; NOT IMPLEMENTED!) The scrollable viewport of a Canvas will respect the wallpaper and page type settings of a Level loaded into it. That is, if the level has a top left edge (not Unbounded) you can NOT scroll to see negative coordinates below (0,0) -- and if the level has a max dimension set, you can't scroll to see pixels outside those dimensions. The Canvas property NoLimitScroll=true will override the scroll locking and let you see outside the bounds, for debugging. - Default map settings for New Level are now: - Page Type: NoNegativeSpace - Wallpaper: notepad.png (default) - MaxWidth: 2550 (8.5" * 300 ppi) - MaxHeight: 3300 ( 11" * 300 ppi)
2018-10-28 05:22:13 +00:00
package wallpaper
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"testing"
)
func TestWallpaper(t *testing.T) {
var testFunc = func(width, height int) {
var (
qWidth = width / 2
qHeight = height / 2
red = color.RGBA{255, 0, 0, 255}
green = color.RGBA{0, 255, 0, 255}
blue = color.RGBA{0, 0, 255, 255}
pink = color.RGBA{255, 0, 255, 255}
)
// Create a dummy image that is width*height and has the four
// quadrants laid out as solid colors:
// Red | Green
// Blue | Pink
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(
// Corner: red
img, // dst Image
Wallpapers and Bounded Levels Implement the Wallpaper system into the levels and the concept of Bounded and Unbounded levels. The first wallpaper image is notepad.png which looks like standard ruled notebook paper. On bounded levels, the top/left edges of the page look as you would expect and the blue lines tile indefinitely in the positive directions. On unbounded levels, you only get the repeating blue lines but not the edge pieces. A wallpaper is just a rectangular image file. The image is divided into four equal quadrants to be the Corner, Top, Left and Repeat textures for the wallpaper. The Repeat texture is ALWAYS used and fills all the empty space behind the drawing. (Doodads draw with blank canvases as before because only levels have wallpapers!) Levels have four options of a "Page Type": - Unbounded (default, infinite space) - NoNegativeSpace (has a top left edge but can grow infinitely) - Bounded (has a top left edge and bounded size) - Bordered (bounded with bordered texture; NOT IMPLEMENTED!) The scrollable viewport of a Canvas will respect the wallpaper and page type settings of a Level loaded into it. That is, if the level has a top left edge (not Unbounded) you can NOT scroll to see negative coordinates below (0,0) -- and if the level has a max dimension set, you can't scroll to see pixels outside those dimensions. The Canvas property NoLimitScroll=true will override the scroll locking and let you see outside the bounds, for debugging. - Default map settings for New Level are now: - Page Type: NoNegativeSpace - Wallpaper: notepad.png (default) - MaxWidth: 2550 (8.5" * 300 ppi) - MaxHeight: 3300 ( 11" * 300 ppi)
2018-10-28 05:22:13 +00:00
image.Rect(0, 0, qWidth, qHeight), // r Rectangle
image.NewUniform(red), // src Image
image.Point{0, 0}, // sp Point
draw.Over, // op Op
)
draw.Draw(
// Top: green
img,
image.Rect(qWidth, 0, width, qHeight),
image.NewUniform(green),
image.Point{qWidth, 0},
draw.Over,
)
draw.Draw(
// Left: blue
img,
image.Rect(0, qHeight, qWidth, height),
image.NewUniform(blue),
image.Point{0, qHeight},
draw.Over,
)
draw.Draw(
// Repeat: pink
img,
image.Rect(qWidth, qHeight, width, height),
image.NewUniform(pink),
image.Point{qWidth, qHeight},
draw.Over,
)
// Output as png to disk if you wanna see what's in it.
if os.Getenv("T_WALLPAPER_PNG") != "" {
fn := fmt.Sprintf("test-%dx%d.png", width, height)
if fh, err := os.Create(fn); err == nil {
defer fh.Close()
if err := png.Encode(fh, img); err != nil {
t.Errorf("err: %s", err)
}
}
}
2023-12-02 20:33:14 +00:00
wp, err := FromImage(img, "dummy")
Wallpapers and Bounded Levels Implement the Wallpaper system into the levels and the concept of Bounded and Unbounded levels. The first wallpaper image is notepad.png which looks like standard ruled notebook paper. On bounded levels, the top/left edges of the page look as you would expect and the blue lines tile indefinitely in the positive directions. On unbounded levels, you only get the repeating blue lines but not the edge pieces. A wallpaper is just a rectangular image file. The image is divided into four equal quadrants to be the Corner, Top, Left and Repeat textures for the wallpaper. The Repeat texture is ALWAYS used and fills all the empty space behind the drawing. (Doodads draw with blank canvases as before because only levels have wallpapers!) Levels have four options of a "Page Type": - Unbounded (default, infinite space) - NoNegativeSpace (has a top left edge but can grow infinitely) - Bounded (has a top left edge and bounded size) - Bordered (bounded with bordered texture; NOT IMPLEMENTED!) The scrollable viewport of a Canvas will respect the wallpaper and page type settings of a Level loaded into it. That is, if the level has a top left edge (not Unbounded) you can NOT scroll to see negative coordinates below (0,0) -- and if the level has a max dimension set, you can't scroll to see pixels outside those dimensions. The Canvas property NoLimitScroll=true will override the scroll locking and let you see outside the bounds, for debugging. - Default map settings for New Level are now: - Page Type: NoNegativeSpace - Wallpaper: notepad.png (default) - MaxWidth: 2550 (8.5" * 300 ppi) - MaxHeight: 3300 ( 11" * 300 ppi)
2018-10-28 05:22:13 +00:00
if err != nil {
t.Errorf("Couldn't create FromImage: %s", err)
t.FailNow()
}
// Check the quarter size is what we expected.
w, h := wp.QuarterSize()
if w != qWidth || h != qHeight {
t.Errorf(
"Got wrong quarter size: expected %dx%d but got %dx%d",
qWidth, qHeight,
w, h,
)
}
// Test the colors.
testColor := func(name string, img *image.RGBA, expect color.RGBA) {
if actual := img.At(5, 5); actual != expect {
t.Errorf(
"%s: expected color %v but got %v",
name,
expect,
actual,
)
}
}
testColor("Corner", wp.Corner(), red)
testColor("Top", wp.Top(), green)
testColor("Left", wp.Left(), blue)
testColor("Repeat", wp.Repeat(), pink)
}
testFunc(128, 128)
testFunc(128, 64)
testFunc(64, 128)
testFunc(12, 12)
testFunc(57, 39)
}