Skip to content

Commit

Permalink
- #1984 wollok-game: Set text on visual components.
Browse files Browse the repository at this point in the history
- #1985 wollok-game: Allow invisible components
  • Loading branch information
lgassman committed Dec 27, 2020
1 parent 1183b94 commit 20095de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.uqbar.project.wollok.game.gameboard.Window
import org.uqbar.project.wollok.interpreter.core.WollokObject

import static extension org.uqbar.project.wollok.game.helpers.WollokConventionExtensions.*
import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*

@Accessors
class VisualComponent {
Expand All @@ -18,20 +19,26 @@ class VisualComponent {

List<BalloonMessage> balloonMessages = newArrayList
boolean showAttributes = false

boolean hasText = false
boolean hasTextColor = false
boolean hasImage = false

WollokObject wObject
WollokObject wPosition

new(WollokObject object) {
wObject = object
wObject?.position // Force evaluate position when is added
this(object, object.position)
}

new(WollokObject object, WollokObject position) {
wObject = object
wPosition = position
hasText = wObject.understands(TEXT_CONVENTION)
hasTextColor = wObject.understands(TEXT_COLOR_CONVENTION)
hasImage = wObject.understands(IMAGE_CONVENTION)
}

def getAttributes() {
wObject.printableVariables.filter[value !== null].map[key + ":" + value.toString].toList
}
Expand All @@ -58,13 +65,24 @@ class VisualComponent {
def void draw(Window window) {
window => [
drawMe
drawText
drawAttributesIfNecesary
drawBalloonIfNecesary
]
}

def drawMe(Window window) {
window.draw(image, position)
if(hasImage) {
window.draw(image, position)
}
}

def drawText(Window window) {
if(hasText) {
val text = wObject.call(TEXT_CONVENTION).wollokToJava(String) as String
val color = hasTextColor ? Color.valueOf(wObject.call(TEXT_COLOR_CONVENTION).wollokToJava(String) as String) : DEFAULT_TEXT_COLOR
window.writeText(text, getPosition(), color)
}
}

def drawAttributesIfNecesary(Window window) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class Window {
def doDraw(Texture texture, Position it, ImageSize size) {
batch.draw(texture, xinPixels, yinPixels, size.width(texture.width), size.height(texture.height))
}

def writeText(String text, Position position, Color color) {
write(text, color, position.xinPixels - 80, position.yinPixels + TEXT_SIZE)
}

def writeAttributes(String text, Position position, Color color) {
val lines = text.split("[\n|\r]").length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package org.uqbar.project.wollok.game.helpers

import com.badlogic.gdx.graphics.Color
import org.uqbar.project.wollok.game.VisualComponent
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper

import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*


class WollokConventionExtensions {

public static val POSITION_CONVENTION = "position"
public static val TEXT_CONVENTION = "text"
public static val TEXT_COLOR_CONVENTION = "textColor"
public static val IMAGE_CONVENTION = "image"
public static val DEFAULT_IMAGE = "wko.png"
public static val DEFAULT_TEXT_COLOR = Color.BLUE


def static asVisual(WollokObject it) { new VisualComponent(it) }
def static asVisualIn(WollokObject it, WollokObject position) { new VisualComponent(it, position) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
// ******************************************
override getThisObject() { this }

def understands(String message, WollokObject... parameters) {
behavior.lookupMethod(message, parameters, false) !== null ||
message.hasProperty && parameters.length() <= 1 ||
message.hasConstantProperty && parameters.empty

}

override call(String message, WollokObject... parameters) {
val method = behavior.lookupMethod(message, parameters, false)
if (method === null) {
Expand Down Expand Up @@ -112,6 +119,10 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
override hasProperty(String variableName) {
properties.contains(variableName)
}

def hasConstantProperty(String variableName) {
constantsProperties.contains(variableName)
}

def throwMessageNotUnderstood(String methodName, Object... parameters) {
try {
Expand Down

0 comments on commit 20095de

Please sign in to comment.