Commit 77c2ec78 authored by Your Name's avatar Your Name

feat(character): Create new character

Create character activity
Store character in database
Add Race table

GTLAB #2
parent 8a29caf5
Pipeline #465 passed with stage
in 0 seconds
......@@ -23,7 +23,7 @@
<PersistentState>
<option name="values">
<map>
<entry key="url" value="jar:file:/opt/android-studio/plugins/android/lib/android.jar!/images/material_design_icons/alert/ic_error_black_24dp.xml" />
<entry key="url" value="jar:file:/opt/android-studio/plugins/android/lib/android.jar!/images/material_design_icons/image/ic_portrait_black_24dp.xml" />
</map>
</option>
</PersistentState>
......@@ -34,7 +34,7 @@
<option name="values">
<map>
<entry key="imageAsset" value="$USER_HOME$/Downloads/icons8-ok-48.png" />
<entry key="outputName" value="ic_failure" />
<entry key="outputName" value="ic_default_avatar" />
</map>
</option>
</PersistentState>
......@@ -51,6 +51,32 @@
</PersistentState>
</value>
</entry>
<entry key="launcherLegacy">
<value>
<PersistentState>
<option name="children">
<map>
<entry key="clipartAsset">
<value>
<PersistentState>
<option name="values">
<map>
<entry key="url" value="jar:file:/opt/android-studio/plugins/android/lib/android.jar!/images/material_design_icons/social/ic_person_black_24dp.xml" />
</map>
</option>
</PersistentState>
</value>
</entry>
</map>
</option>
<option name="values">
<map>
<entry key="outputName" value="ic_default_avatar" />
</map>
</option>
</PersistentState>
</value>
</entry>
<entry key="notification">
<value>
<PersistentState>
......@@ -76,7 +102,7 @@
</option>
<option name="values">
<map>
<entry key="outputIconType" value="ACTIONBAR" />
<entry key="outputIconType" value="LAUNCHER_LEGACY" />
</map>
</option>
</PersistentState>
......
......@@ -19,6 +19,13 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CharacterCreation"
android:label="@string/title_activity_character_creation"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
\ No newline at end of file
package fr.yapbreak.skyrimquest;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import fr.yapbreak.skyrimquest.character.CharacterSaver;
public class CharacterCreation extends AppCompatActivity {
private ImageView avatarView;
private EditText nameView;
private Spinner raceSpinner;
private EditText levelView;
ArrayList<String> raceIdArray;
private final static int LOAD_AVATAR = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_creation);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
avatarView = findViewById(R.id.iv_avatar);
nameView = findViewById(R.id.et_name);
raceSpinner = findViewById(R.id.sp_race);
levelView = findViewById(R.id.et_level);
Button levelMinus = findViewById(R.id.bt_level_minus);
Button levelPlus = findViewById(R.id.bt_level_plus);
Button create = findViewById(R.id.bt_character_create);
levelView.setText("1");
levelMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int level = Integer.parseInt(levelView.getText().toString());
if (level > 1)
level--;
levelView.setText(String.format("%s", level));
}
});
levelPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int level = Integer.parseInt(levelView.getText().toString());
level++;
levelView.setText(String.format("%s", level));
}
});
Cursor raceCursor = QuestList.getInstance().getQuest_data().getAllRaces();
ArrayList<String> raceSelection = new ArrayList<>();
raceIdArray = new ArrayList<>();
raceCursor.moveToFirst();
do {
String resId = raceCursor.getString(1);
String race;
try {
Field translatedRace = R.string.class.getDeclaredField(resId);
race = getString(translatedRace.getInt(translatedRace));
} catch (Exception e) {
race = resId;
}
raceSelection.add(race);
raceIdArray.add(resId);
raceCursor.moveToNext();
} while (!raceCursor.isAfterLast());
ArrayAdapter<String> raceSpinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, raceSelection);
raceSpinner.setAdapter(raceSpinnerAdapter);
avatarView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, LOAD_AVATAR);
}
});
create.setOnClickListener(new CharacterSaver(this));
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case LOAD_AVATAR:
Uri selectedAvatar = data.getData();
if (selectedAvatar == null) {
return;
}
try {
int width = avatarView.getWidth();
int height = avatarView.getHeight();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedAvatar);
Bitmap myAvatar = Bitmap.createScaledBitmap(bitmap, width, height, false);
avatarView.setImageBitmap(myAvatar);
} catch (IOException e) {
Log.i("SKYRIM", selectedAvatar.toString() + " not found");
}
break;
}
}
}
public String getName()
{
return nameView.getText().toString();
}
public String getRaceId()
{
return raceIdArray.get(raceSpinner.getSelectedItemPosition());
}
public int getLevel()
{
return Integer.parseInt(levelView.getText().toString());
}
public Bitmap getAvatar()
{
BitmapDrawable d = (BitmapDrawable) avatarView.getDrawable();
return d.getBitmap();
}
}
package fr.yapbreak.skyrimquest;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
......@@ -17,14 +18,23 @@ import fr.yapbreak.skyrimquest.quests.SkyrimCursorAdapter;
public class QuestList extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected static QuestList instance;
private DataHelper quest_data;
public static QuestList getInstance()
{
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
// TODO: remove this line when database is ready
this.deleteDatabase("quest_database");
quest_data = new DataHelper(this);
quest_data.populate();
......@@ -74,6 +84,10 @@ public class QuestList extends AppCompatActivity
//noinspection SimplifiableIfStatement
if (id == R.id.action_character_switch) {
return true;
} else if (id == R.id.action_character_create) {
Intent i = new Intent(this, CharacterCreation.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
......@@ -103,4 +117,9 @@ public class QuestList extends AppCompatActivity
drawer.closeDrawer(GravityCompat.START);
return true;
}
public DataHelper getQuest_data()
{
return quest_data;
}
}
package fr.yapbreak.skyrimquest.character;
import java.lang.reflect.Field;
import fr.yapbreak.skyrimquest.QuestList;
import fr.yapbreak.skyrimquest.R;
import fr.yapbreak.skyrimquest.data.DataHelper;
public class Character {
private String name;
private String avatarFileName;
private int level;
private int id;
private int raceId;
public Character(int id) throws DataHelper.DataNotFound
{
this.id = id;
QuestList.getInstance().getQuest_data().FillCharacter(id, this);
}
public Character(String name,
String avatarFileName,
int level,
String raceId)
{
this.name = name;
this.avatarFileName = avatarFileName;
this.level = level;
try {
this.raceId = QuestList.getInstance().getQuest_data().getRaceId(raceId);
} catch (DataHelper.DataNotFound e) {
this.raceId = -1;
}
this.id = QuestList.getInstance().getQuest_data().getCharacterId(this);
}
public String getName()
{
return name;
}
public String getAvatarFileName()
{
return avatarFileName;
}
public int getLevel()
{
return level;
}
public int getRaceId()
{
return raceId;
}
public String getRace()
{
String raceIdString;
try {
raceIdString = QuestList.getInstance().getQuest_data().getRaceIdString(raceId);
} catch (DataHelper.DataNotFound e) {
return Integer.toString(raceId);
}
String race;
try {
Field translatedRace = R.string.class.getDeclaredField(raceIdString);
race = QuestList.getInstance().getString(translatedRace.getInt(translatedRace));
} catch (Exception e) {
race = raceIdString;
}
return race;
}
public int getId()
{
return id;
}
void store()
{
this.id = QuestList.getInstance().getQuest_data().storeCharacter(this);
}
public void setName(String name)
{
this.name = name;
}
public void setLevel(int level)
{
this.level = level;
}
public void setAvatarFileName(String avatarFileName)
{
this.avatarFileName = avatarFileName;
}
public void setRaceId(int raceId)
{
this.raceId = raceId;
}
}
package fr.yapbreak.skyrimquest.character;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import fr.yapbreak.skyrimquest.CharacterCreation;
public class CharacterSaver implements View.OnClickListener {
private CharacterCreation activity;
public CharacterSaver(CharacterCreation activity) {
this.activity = activity;
}
@Override
public void onClick(View view) {
String name = activity.getName();
String race = activity.getRaceId();
int level = activity.getLevel();
Bitmap avatar = Bitmap.createBitmap(activity.getAvatar());
File avatarFile = new File(view.getContext().getFilesDir(), UUID.randomUUID().toString() + ".png");
try {
FileOutputStream avatarStream = new FileOutputStream(avatarFile);
avatar.compress(Bitmap.CompressFormat.PNG, 95, avatarStream);
avatarStream.flush();
avatarStream.close();
} catch (IOException e) {
Log.e("SKYRIM", "Fail to create avatar image");
e.printStackTrace();
}
Character c = new Character(name, race, level, avatarFile.getAbsolutePath());
c.store();
activity.finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CharacterCreation">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_character_creation" />
</android.support.design.widget.CoordinatorLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_character_creation"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".CharacterCreation"
tools:showIn="@layout/activity_character_creation">
<ImageView
android:id="@+id/iv_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:src="@mipmap/ic_default_avatar"
app:layout_constraintBottom_toBottomOf="@id/sp_race"/>
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:padding="20dp"
android:text="@string/character_name_label"
app:layout_constraintLeft_toRightOf="@id/iv_avatar"
app:layout_constraintTop_toTopOf="@id/iv_avatar"
tools:layout_editor_absoluteX="152dp"
tools:layout_editor_absoluteY="2dp" />
<Spinner
android:id="@+id/sp_race"
android:layout_width="216dp"
android:layout_height="18dp"
app:layout_constraintTop_toBottomOf="@id/et_name"
app:layout_constraintLeft_toRightOf="@id/iv_avatar" />
<TextView
android:id="@+id/tv_level_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/level_label"
app:layout_constraintTop_toBottomOf="@id/iv_avatar"
/>
<Button
android:id="@+id/bt_level_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintTop_toBottomOf="@id/tv_level_label" />
<EditText
android:id="@+id/et_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
app:layout_constraintLeft_toRightOf="@id/bt_level_minus"
app:layout_constraintTop_toBottomOf="@id/tv_level_label" />
<Button
android:id="@+id/bt_level_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintLeft_toRightOf="@id/et_level"
app:layout_constraintTop_toBottomOf="@id/tv_level_label"
/>
<Button
android:id="@+id/bt_character_create"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/character_create"
app:layout_constraintTop_toBottomOf="@id/et_level"
app:layout_constraintLeft_toLeftOf="@id/activity_character_creation"
app:layout_constraintRight_toRightOf="@id/activity_character_creation"
android:layout_margin="50dp"
android:gravity="center"/>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
......@@ -6,4 +6,9 @@
android:orderInCategory="100"
android:title="@string/action_character_switch"
app:showAsAction="never" />
<item
android:id="@+id/action_character_create"
android:orderInCategory="200"
android:title="@string/action_character_create"
app:showAsAction="never" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Skyrim Quest</string>
<string name="action_character_switch">Changer</string>
<string name="quest_before_storm">Avant la tempête</string>
<string name="quest_before_storm_desc">Informer le Jarl de Blancherive de l’attaque du dragon à Helgen</string>
......@@ -17,4 +16,19 @@
<string name="quest_unbound_desc">Échapper à l’empire et à l’attaque d’un dragon</string>
<string name="quest_way_of_voice">L’art de la voix</string>
<string name="quest_way_of_voice_desc">Rencontrer les Grises Barbes</string>
<string name="title_activity_character_creation">Création de personnage</string>
<string name="character_name_label">Nom</string>
<string name="level_label">Niveau</string>
<string name="character_create">Création</string>
<string name="action_character_create">Nouveau…</string>
<string name="race_altmer">Altmer (Haut Elfe)</string>
<string name="race_argonian">Argonien</string>
<string name="race_bosmer">Bosmer (Elfe des Bois)</string>
<string name="race_breton">Breton</string>
<string name="race_dunmer">Dunmer (Elfe Noir)</string>
<string name="race_imperial">Impérial</string>
<string name="race_khajiit">Khajiit</string>
<string name="race_nord">Nordique</string>
<string name="race_orsimer">Orsimer (Orc)</string>
<string name="race_redguard">Rougegarde</string>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="race_altmer">Altmer (High Elf)</string>
<string name="race_argonian">Argonian</string>
<string name="race_bosmer">Bosmer (Wood Elf)</string>
<string name="race_breton">Breton</string>
<string name="race_dunmer">Dunmer (Dark Elf)</string>
<string name="race_imperial">Imperial</string>
<string name="race_khajiit">Khajiit</string>
<string name="race_nord">Nord</string>
<string name="race_orsimer">Orsimer (Orc)</string>
<string name="race_redguard">Redguard</string>
</resources>
......@@ -11,4 +11,11 @@
<string name="quest_img_alt">Image related to current quest</string>
<string name="quest_summary_default">Details on quest…</string>
<string name="quest_title_default">Quest Title</string>
<!-- Strings from Character creation activity -->
<string name="title_activity_character_creation">Character Creation</string>
<string name="character_name_label">Name</string>
<string name="level_label">Level</string>
<string name="character_create">Create</string>
<string name="action_character_create">Create new…</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment