Skip to content

Commit

Permalink
added retro controls, IScreen, fonts, Util.js wrapper for js.
Browse files Browse the repository at this point in the history
  • Loading branch information
asalga committed Jul 25, 2013
1 parent 7981724 commit debcb00
Show file tree
Hide file tree
Showing 13 changed files with 5,131 additions and 2,537 deletions.
5,011 changes: 3,435 additions & 1,576 deletions Horadrix-min.pde

Large diffs are not rendered by default.

990 changes: 30 additions & 960 deletions Horadrix.pde

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions IScreen.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public interface IScreen{

public void draw();
public void update();

// Mouse methods
public void mousePressed();
public void mouseReleased();
public void mouseDragged();
public void mouseMoved();

public String getName();

public boolean isAlive();
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
build: minify

minify:
cat Debugger.pde Keyboard.pde Ticker.pde TokenType.pde Utils.pde GemToken.pde Horadrix.pde SoundManager.pde Token.pde Tuple.pde AssetStore.java > Horadrix-min.pde
cat IScreen.pde SpriteSheetLoader.pde ScreenGameplay.pde ScreenSplash.pde Debugger.pde RetroFont.pde RetroLabel.pde RetroPanel.pde RetroWidget.pde Keyboard.pde Ticker.pde TokenType.pde GemToken.pde Horadrix.pde SoundManager.pde Token.pde Tuple.pde AssetStore.java > Horadrix-min.pde IScreen.pde ScreenSplash.pde Utils.js ScreenGameplay.pde

publish: minify
git add Horadrix-min.js
Expand Down
99 changes: 99 additions & 0 deletions RetroFont.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
*/
public class RetroFont{

private PImage chars[];
private PImage trimmedChars[];

private int glyphWidth;
private int glyphHeight;

/*
inImage
*/
private PImage truncateImage(PImage inImage, char ch){

int startX = 0;
int endX = inImage.width - 1;
int x, y;

// Find the starting X coord of the image.
for(x = inImage.width; x >= 0 ; x--){
for(y = 0; y < inImage.height; y++){

color testColor = inImage.get(x,y);
if( alpha(testColor) > 0.0){
startX = x;
}
}
}

// Find the ending coord
for(x = 0; x < inImage.width; x++){
for(y = 0; y < inImage.height; y++){

color testColor = inImage.get(x,y);
if( alpha(testColor) > 0.0){
endX = x;
}
}
}

return inImage.get(startX, 0, endX-startX+1, inImage.height);
}


// Do not instantiate directly
public RetroFont(String imageFilename, int glyphWidth, int glyphHeight, int borderSize){
this.glyphWidth = glyphWidth;
this.glyphHeight = glyphHeight;

PImage fontSheet = loadImage(imageFilename);

chars = new PImage[96];
trimmedChars = new PImage[96];

int x = 0;
int y = 0;

//
//
for(int currChar = 0; currChar < 96; currChar++){
chars[currChar] = fontSheet.get(x, y, glyphWidth, glyphHeight);
trimmedChars[currChar] = truncateImage(fontSheet.get(x, y, glyphWidth, glyphHeight), (char)currChar);

x += glyphWidth + borderSize;
if(x >= fontSheet.width){
x = 0;
y += glyphHeight + borderSize;
}
}


// For each character, truncate the x margin
//for(int currChar = 0; currChar < 96; currChar++){
//chars[currChar] = truncateImage( chars[currChar] );
//}
}

//public static void create(String imageFilename, int charWidth, int charHeight, int borderSize){
//PImage fontSheet = loadImage(imageFilename);
public PImage getGlyph(char ch){
int asciiCode = Utils.charCodeAt(ch);

return chars[asciiCode-32];
}

public PImage getTrimmedGlyph(char ch){
int asciiCode = Utils.charCodeAt(ch);
return trimmedChars[asciiCode-32];
}

public int getGlyphWidth(){
return glyphWidth;
}

public int getGlyphHeight(){
return glyphHeight;
}
}
182 changes: 182 additions & 0 deletions RetroLabel.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
*
*/
public class RetroLabel extends RetroPanel{

public static final int JUSTIFY_MANUAL = 0;
public static final int JUSTIFY_LEFT = 1;
//public static final int JUSTIFY_RIGHT = 1;

private final int NEWLINE = 10;
private boolean dirty;
private String text;
private RetroFont font;

//
private int horizontalSpacing;
private int verticalSpacing;
private boolean horizontalTrimming;

private int justification;

public RetroLabel(RetroFont font){
setFont(font);
setVerticalSpacing(1);
setHorizontalSpacing(1);
//setJustification(JUSTIFY_LEFT);
setHorizontalTrimming(false);
}

public void setHorizontalTrimming(boolean horizTrim){
horizontalTrimming = horizTrim;
}

/**
*/
public void setHorizontalSpacing(int spacing){
horizontalSpacing = spacing;
dirty = true;
}

public void setVerticalSpacing(int spacing){
verticalSpacing = spacing;
dirty = true;
}

/*
* Will immediately calculate the width
*/
public void setText(String text){
this.text = text;
dirty = true;

int newWidth = 0;
for(int letter = 0; letter < text.length(); letter++){

PImage glyph = getGlyph(text.charAt(letter));

if(glyph != null){
newWidth += glyph.width + horizontalSpacing;
}
}
println("new width: " + newWidth);
w = newWidth;
}

public void setFont(RetroFont font){
this.font = font;
dirty = true;
h = this.font.getGlyphHeight();
}

/**
*/
private int getWordWidth(String word){
return word.length() * font.getGlyphWidth() + (word.length() * horizontalSpacing );
}

//public void setJustification(int justify){
// justification = justify;
//}

/**
Draws the text in the label depending on the
justification of the panel is sits inside
*/
public void draw(){

// Return if there is nothing to draw
if(text == null || visible == false){
return;
}

if(justification == JUSTIFY_MANUAL){
int currX = x;
int lineIndex = 0;

for(int letter = 0; letter < text.length(); letter++){
if((int)text.charAt(letter) == NEWLINE){
lineIndex++;
currX = x;
continue;
}
PImage glyph = getGlyph(text.charAt(letter));

if(glyph != null){
image(glyph, currX, y + lineIndex * (font.getGlyphHeight() + verticalSpacing));

currX += glyph.width + horizontalSpacing;
}
}
currX += font.getGlyphWidth();
}
else{

// iterate over each word and see if it would fit into the
// panel. If not, add a line break
String[] words = text.split(" ");
int[] firstCharIndex = new int[words.length];

// get indices of fist char of every word
// for(int word = 0; word < words.length; word++){
// firstCharIndex[word] =
//}
firstCharIndex[0] = 0;

int iter = 1;
for(int letter = 0; letter < text.length(); letter++){
if(text.charAt(letter) == ' '){
firstCharIndex[iter] = letter + 1;
iter++;
}
}

int[] wordWidths;

// start drawing at the panel
int currXPos = x;

int lineIndex = 0;

// Iterate over all the words
for(int word = 0; word < words.length; word++){
int wordWidth = getWordWidth(words[word]);

if(justification == JUSTIFY_LEFT){
if(word != 0 && currXPos + wordWidth + 0 > parent.getWidth() ){
lineIndex++;
currXPos = x;
}
}

// Iterate over the letter of each word
for(int letter = 0; letter < words[word].length(); letter++){

int firstChar = firstCharIndex[word];

//
if((int)words[word].charAt(letter) == NEWLINE){
lineIndex++;
currXPos = x;
continue;
}

PImage glyph = getGlyph(words[word].charAt(letter));

if(glyph != null){
image(glyph, currXPos, lineIndex * (font.getGlyphHeight() + verticalSpacing));
currXPos += font.getGlyphWidth() + horizontalSpacing;
}
}
currXPos += font.getGlyphWidth();
}
}
}

public PImage getGlyph(char ch){
if(horizontalTrimming == true){
return font.getTrimmedGlyph(ch );
}
return font.getGlyph( ch);
}
}
77 changes: 77 additions & 0 deletions RetroPanel.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
*/
public class RetroPanel extends RetroWidget{

ArrayList<RetroWidget> children;

public RetroPanel(){
w = 0;
h = 0;
x = 0;
y = 0;
removeAllChildren();
}

public void removeAllChildren(){
children = new ArrayList<RetroWidget>();
}

public void addWidget(RetroWidget widget){
widget.setParent(this);
children.add(widget);
}

public void setWidth(int w){
this.w = w;
}

public int getWidth(){
return w;
}

public RetroPanel(int x, int y, int w, int h){
removeAllChildren();
this.w = w;
this.h = h;
this.x = x;
this.y = y;
}

public void pixelsFromTop(int pixels){
x = width/2 - 60/2;
//startY = 0;
}

public void pixelsFromBottomLeft(int bottomPixels, int leftPixels){
y = parent.y + parent.h - h + bottomPixels;
x = parent.x + leftPixels;
}

/**
*/
public void pixelsFromCenter(int xPixels, int yPixels){
x = (parent.w/2) - (w/2) + xPixels;
y = (parent.h/2) - (h/2) + yPixels;
}


public void draw(){

pushStyle();
noFill();
stroke(255, 0, 0,32);
rect(x, y, w, h);
popStyle();

if(visible == false || children.size() == 0){
return;
}

pushMatrix();
translate(x,y);
for(int i = 0; i < children.size(); i++){
children.get(i).draw();
}
popMatrix();
}
}
Loading

0 comments on commit debcb00

Please sign in to comment.