Skip to content

Commit

Permalink
Team screen widget in charge
Browse files Browse the repository at this point in the history
  • Loading branch information
Aksenov239 committed Dec 1, 2015
1 parent 32121c2 commit c26b1df
Show file tree
Hide file tree
Showing 17 changed files with 539 additions and 28 deletions.
93 changes: 93 additions & 0 deletions src/main/java/ru/ifmo/acm/backend/player/PlayerInImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ru.ifmo.acm.backend.player;

import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.direct.BufferFormat;
import uk.co.caprica.vlcj.player.direct.BufferFormatCallback;
import uk.co.caprica.vlcj.player.direct.DirectMediaPlayer;
import uk.co.caprica.vlcj.player.direct.RenderCallbackAdapter;
import uk.co.caprica.vlcj.player.direct.format.RV32BufferFormat;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

/**
* @author: pashka
*/
public class PlayerInImage {

private final BufferedImage image;
private JComponent frame;
private final MediaPlayer mediaPlayer;

public PlayerInImage(int width, int height, JComponent frame, String media) {
image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height);
image.setAccelerationPriority(1.0f);
this.frame = frame;
MediaPlayerFactory factory = new MediaPlayerFactory(new String[0]);
mediaPlayer = factory.newDirectMediaPlayer(new TestBufferFormatCallback(), new TestRenderCallback());
mediaPlayer.setRepeat(true);
// mediaPlayer.addMediaOptions(":file-caching=1500");
// mediaPlayer.setStandardMediaOptions(":file-caching=1500");
// mediaPlayer.setVolume(0);
if (media != null)
mediaPlayer.playMedia(media, ":file-caching=0");
System.err.println("PLAY!!! " + media);
}

public void stop() {
mediaPlayer.stop();
mediaPlayer.release();
}

public JComponent getComponent() {
return frame;
}

public void setComponent(JComponent frame) {
this.frame = frame;
}

public MediaPlayer getPlayer() {
return mediaPlayer;
}

public BufferedImage getImage() {
return image;
}

private final class TestRenderCallback extends RenderCallbackAdapter {

public TestRenderCallback() {
super(((DataBufferInt) image.getRaster().getDataBuffer()).getData());
}

@Override
public void onDisplay(DirectMediaPlayer mediaPlayer, int[] data) {
// The image data could be manipulated here...

/* RGB to GRAYScale conversion example */
// for(int i=0; i < data.length; i++){
// int argb = data[i];
// int b = (argb & 0xFF);
// int g = ((argb >> 8 ) & 0xFF);
// int r = ((argb >> 16 ) & 0xFF);
// int grey = (r + g + b + g) >> 2 ; //performance optimized - not real grey!
// data[i] = (grey << 16) + (grey << 8) + grey;
// }
if (frame != null)
frame.repaint();
}
}

private final class TestBufferFormatCallback implements BufferFormatCallback {

@Override
public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) {
return new RV32BufferFormat(image.getWidth(), image.getHeight());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.util.Properties;
import ru.ifmo.acm.backend.player.TickPlayer;

/**
* Created by aksenov on 14.04.2015.
Expand All @@ -26,14 +27,20 @@ public MainScreenGenerator() throws IOException {
long timeAdvertisement = Long.parseLong(properties.getProperty("advertisement.time"));
long timePerson = Long.parseLong(properties.getProperty("person.time"));

widgets = new Widget[6];
widgets = new Widget[7];
widgets[0] = new GreenScreenWidget();
widgets[0].setVisible(true);
widgets[1] = new ClockWidget(updateWait);
widgets[2] = new CreepingLineWidget(updateWait);
widgets[3] = new DoublePersonWidget(updateWait, timePerson);
widgets[4] = new AdvertisementWidget(updateWait, timeAdvertisement);
widgets[5] = new StandingsWidget(updateWait);
widgets[1] = new TeamInfoWidget(
updateWait,
width,
height - (int)(32 * TickPlayer.scale),
Integer.parseInt(properties.getProperty("sleep.time"))
);
widgets[2] = new ClockWidget(updateWait);
widgets[3] = new CreepingLineWidget(updateWait);
widgets[4] = new DoublePersonWidget(updateWait, timePerson);
widgets[5] = new AdvertisementWidget(updateWait, timeAdvertisement);
widgets[6] = new StandingsWidget(updateWait);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.ifmo.acm.backend.player.widgets;

/**
* Created by aksenov on 28.04.2015.
*/
public interface PlayerWidget {
public void change(String url);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ru.ifmo.acm.backend.player.widgets;

import ru.ifmo.acm.backend.net.Preparation;
import ru.ifmo.acm.datapassing.Data;

import java.awt.*;

/**
* @author: pashka
*/
public class TeamInfoWidget extends TeamWidget {
private int teamId;

public TeamInfoWidget(long updateWait, int width, int height, int sleepTime) {
super(0, 0, width, height, sleepTime);

this.updateWait = updateWait;

teamId = -1;
}

private long updateWait;
private long lastUpdate;

public void update() {
if (lastUpdate + updateWait < System.currentTimeMillis()) {
Data data = Preparation.dataLoader.getDataBackend();
if (data == null) {
return;
}

//System.err.println(data.teamData.isTeamVisible);

if (!data.teamData.isTeamVisible) {
setVisible(false);
stop();
} else {
setVisible(true);
if (!isVisible() || (data.teamData.teamId != teamId && !inChange.get())) {
//System.err.println("Change to " + urlTemplates.get(data.teamData.infoType) + " " + data.teamData.teamId);
change(String.format(urlTemplates.get(data.teamData.infoType), data.teamData.teamId));
teamId = data.teamData.teamId;
}
}


lastUpdate = System.currentTimeMillis();
}
}

public void paint(Graphics2D g, int width, int height) {
update();
super.paint(g, width, height);
}

protected int getTeamId() {
return teamId;
}
}
158 changes: 158 additions & 0 deletions src/main/java/ru/ifmo/acm/backend/player/widgets/TeamWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package ru.ifmo.acm.backend.player.widgets;

import ru.ifmo.acm.backend.net.Preparation;
import ru.ifmo.acm.backend.player.TickPlayer;
import ru.ifmo.acm.events.RunInfo;
import ru.ifmo.acm.events.TeamInfo;

import java.awt.*;
import java.util.Arrays;
import java.util.Properties;
import java.util.HashMap;

/**
* @author: pashka
*/
public abstract class TeamWidget extends VideoWidget {
public static String[] types;
public static HashMap<String, String> urlTemplates;

static {
Properties properties = new Properties();
try {
properties.load(TeamWidget.class.getClassLoader().getResourceAsStream("mainscreen.properties"));
types = properties.getProperty("info.types", "screen;camera;info").split(";");
urlTemplates = new HashMap<>();
for (int i = 0; i < types.length; i++) {
String url = properties.getProperty("info." + types[i], "");
urlTemplates.put(types[i], url);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public TeamWidget(int x, int y, int width, int height, int sleepTime) {
super(x, y, width, height, sleepTime);
}

protected abstract int getTeamId();

protected Font FONT1 = Font.decode("Open Sans Italic " + (int) (40 * TickPlayer.scale));

protected int X = (int) (20 * TickPlayer.scale);
protected int Y = (int) (20 * TickPlayer.scale);
protected int GAP_Y = (int) (5 * TickPlayer.scale);
protected int GAP_X = (int) (5 * TickPlayer.scale);
protected int PR_WIDTH = (int) (50 * TickPlayer.scale);
protected int RUN_WIDTH = (int) (80 * TickPlayer.scale);
protected int RUN_SMALL_WIDTH = (int) (20 * TickPlayer.scale);
protected int HEIGHT = (int) (45 * TickPlayer.scale);
protected double STAR_SIZE = 5 * TickPlayer.scale;
Font FONT2 = Font.decode("Open Sans Italic " + (int) (30 * TickPlayer.scale));

private static final Color GREEN = new Color(27, 155, 82);//Color.decode("0x33ff00");
private static final Color RED = Color.decode("0xaa0000");
private static final Color YELLOW = new Color(250, 200, 82);//Color.decode("0x33ff00");
private TeamInfo team;

public static final int PERIOD = 500;

private double getTimeOpacity() {
long time = System.currentTimeMillis();
long second = time / PERIOD;
long percent = time % PERIOD;

double v = percent * 1.0 / PERIOD;
return (second % 2 == 0 ? v : 1 - v) / 2 + 0.5;
}

private void drawReplay(Graphics2D g, int x, int y, int width, int height) {
g.setFont(FONT2);
drawTextInRect(g, "R", (int) (x + width * 0.95), (int) (y + height * 0.17), -1, HEIGHT, POSITION_CENTER, RED, Color.WHITE, getTimeOpacity());
}

@Override
public void paint(Graphics2D g, int width, int height) {
if (!isVisible())
return;

g.drawImage(image, x, y, null);
if (inChange.get() || team == null) {
team = Preparation.eventsLoader.getContestData().getParticipant(getTeamId());
inChange.set(false);
///}
if (URL.contains("info")) {
return;
}
if (team == null) return;
g.setFont(FONT1);
int dx = (int) (this.width * 0.52);
int dy = (int) (this.height * 0.9);
drawTeamPane(g, team, x + dx, y + dy, (int) (this.width * 0.45), 1);

g.setFont(FONT2);
for (int i = 0; i < team.getRuns().length; i++) {
int y = Y + (HEIGHT + GAP_Y) * i;
RunInfo[] runs = team.getRuns()[i].toArray(new RunInfo[0]);

Color problemColor = MAIN_COLOR;
for (int j = 0; j < runs.length; j++) {
RunInfo run = runs[j];
if (run.getResult().equals("AC")) {
problemColor = GREEN;
break;
}
if (run.getResult().equals("")) {
problemColor = YELLOW;
} else {
problemColor = RED;
}
}

drawTextInRect(g, "" + (char) ('A' + i), this.x + X, this.y + y,
PR_WIDTH, HEIGHT, POSITION_CENTER, problemColor, Color.WHITE, 1);

int x = X + PR_WIDTH + GAP_X;
for (int j = 0; j < runs.length; j++) {
RunInfo run = runs[j];
Color color = run.getResult().equals("AC") ? GREEN : run.getResult().equals("") ? YELLOW : RED;
if (j == runs.length - 1) {
drawTextInRect(g, format(run.getTime()), this.x + x, this.y + y,
RUN_WIDTH, HEIGHT, POSITION_CENTER, color, Color.WHITE, 1);
//System.err.println(Arrays.toString(Preparation.eventsLoader.getContestData().firstTimeSolved()));
if (run.getResult().equals("AC") && run.getTime() == Preparation.eventsLoader.getContestData().firstTimeSolved()[run.getProblemNumber()]) {
drawStar(g, this.x + x + RUN_WIDTH, (int) (this.y + y + STAR_SIZE / 2));
}
x += RUN_WIDTH + GAP_X;
} else if (run.getTime() != runs[j + 1].getTime()) {
drawTextInRect(g, "", this.x + x, this.y + y,
RUN_SMALL_WIDTH, HEIGHT, POSITION_CENTER, color, Color.WHITE, 1);
x += RUN_SMALL_WIDTH + GAP_X;
}

}
}
}

private void drawStar(Graphics2D g, int x, int y) {
g.setColor(Color.YELLOW);
int[] xx = new int[10];
int[] yy = new int[10];
double[] d = {STAR_SIZE, STAR_SIZE * 2};
for (int i = 0; i < 10; i++) {
xx[i] = (int) (x + Math.sin(Math.PI * i / 5) * d[i % 2]);
yy[i] = (int) (y + Math.cos(Math.PI * i / 5) * d[i % 2]);
}
g.fillPolygon(new Polygon(xx, yy, 10));
}

private String format(double time) {
int s = (int) time;
int m = s / 60;
s %= 60;
int h = m / 60;
m %= 60;
return String.format("%d:%02d", h, m);
}
}
Loading

0 comments on commit c26b1df

Please sign in to comment.